Title with ampersands escaped with link_to - how to prevent this? - ruby

I am using Ruby link_to in a .erb file to link to a story. The title has an ampersand in it. I want the text displayed for title to show the ampersand and not be escaped.
My data is:
story = {"title"=>"Dungeons & Dragons has its own version of the Slender Man now", "url"=>"https://www.polygon.com/22436057/dungeons-dragons-slender-man-bagman-van-richtens-guide-to-ravenloft"}
My html code has:
<h4><%= link_to story[:title], story[:url], { 'data-analytics-link' => 'readmore' } %></h4>
Here is what the link looks like with the ampersand:
Dungeons & Dragons has its own version of the Slender Man now
I have tried updating the link_to to be:
<h4><%= link_to h(story[:title]), story[:url], { 'data-analytics-link' => 'readmore' } %></h4>
but I get the same result.
How can I use link_to so that the text for link shows the ampersand?

Related

Link_to with a custom method and adding html options

Could anyone advise how to set out the following link_to please, i thought that any html option ( and that includes a class doesnt it?) should be in {}...
So i have this link_to at the moment
<%= link_to(#portfolio.previous_post) if #portfolio.previous_post %>
I would like to add a class of 'prev' as this has a background image within the css, rather than using text..I am unsure on where to put
:class => 'prev' or class: 'prev'
depending on preference of course
This doesn't work
<%= link_to(#portfolio.previous_post, { class: 'prev'}) if #portfolio.previous_post %>
Any help appreciated
Something like this should work:
` <% if #portfolio.previous_post %>
<%= link_to "link_name",#portfolio.previous_post, class: "prev"%>
<% end %>`
This should work as well:
` <%= link_to_if(#portfolio.previous_post, "link_name", #portfolio.previous_post,
class: "prev") %>`
Once you provide a name for the url in addition to the url, everything seems to work.

Rails 4 - Controller Edit Action Returning Wrong Record

I have a Comment model which belongs to both User and Story. Creating a comment correctly associated to the appropriate User and Story is working fine but when trying to edit the comment my edit action appears to retrieving the wrong record.
The offending action in comments_controller.rb:
def edit
#story = Story.find_by(params[:story_id])
#comment = #story.comments.find_by(params[:id])
end
The link used to render the comments/edit view:
<%= link_to 'edit', edit_story_comment_path(comment.story_id, comment.id) %>
The corresponding view:
<%= form_for(#comment, url: { controller: 'comments', action: 'update' }) do |f| %>
<%= f.text_area :content %>
<%= f.submit "update" %>
<% end %>
The edit view appears to be rendering the most recently added comment regardless of which #comment I am trying to edit.
You're using find_by, which is basically a magic find_by_X method, with no fields specified. find_by(1) generates invalid SQL for me using Postgres, but it might be that whatever database back-end your using accepts it.
Regardless, find_by certainly won't do what you want it to do.
You should be using find, if you want to find records by id:
#story = Story.find(params[:story_id])

rails sending code block to partial

Rails 3.2.13 & ERB
I am trying to get send some link_to items to a partial. I am sending in a title to the partial successfully as below.
<%= render :partial =>'form',
:locals => {:page_title => t(:'<h2>Editing Feature</h2>')}
What I dont like is that I am currently doing this:
<%= link_to 'Show', #feature %> |
<%= link_to 'Back', features_path %>
as part of the primary page. I would like to take this codeblock and send it to the partial for rendering there. The primary reason is the simple_form is defined in the partial and I have a well that contains everything on that page. Except for the two link_to items down on the bottom of the page. If I could pass them into the partial somehow (I assume as a code block) then I can decide where to place them and how to render them in the form itself instead of kind of as an afterthought.
Thanks.
Typically we use partials to break off HTML chunks into separate modular components and reduce repetition. Partials have the same variable context as the template that rendered them, so as-is, you wouldn't have any trouble simply relocating your links to your partial and everything should just work.
I think the better practice, however, would be to pass your instance variable as a local to reduce coupling with your controller. Something like:
Your view:
<%= render :partial =>'form',
:locals => {:page_title => t(:'<h2>Editing Feature</h2>'),
:feature => #feature} %>
_form.html.erb:
<%= link_to 'Show', feature %> |
<%= link_to 'Back', features_path %>
This way, if you were to render the partial elsewhere in your application you don't need to have an instance variable handy. An example situation where this could be useful would be looping through multiple "features" (a la an index view) and spitting out the relevant HTML as defined in the partial.

rails change path of partial collection rendering

I have an STI relationship where a conversation is composed of both messages and images.
Now when I go to render them i use:
<%= render conversation %>
which works perfect. It finds the given template for the given object and renders it.
Now for my mobile site I want to use the same thing only now it should find say:
/mobile/message/_message.html.erb
instead of
/message/_message.html.erb
So in my controller i said:
if mobile?
prepend_view_path "mobile"
end
Which does get called, and it "prepends my view path" which i can see is working when i do:
raise view_paths.inspect
However now when i do my
<%= render conversation %>
It is still looking in the default location i.e. /views/ for the partial
Well, this should work, but it is distressingly inelegant:
<% conversation.each do |c| %>
<% c_class = c.class.to_s.downcase.underscore %>
<%= render :partial => "mobile/#{c_class}/#{c_class}", :object => c %>
<% end %>

Rails 3 create a link to image_url and show image_url(:medium)

I am trying to create a link to an image #user.image_url. This will be the full sized image. I want the medium resized image to be shown #user.image_url(:medium). But what I also want is for the #user.image_url to open in FancyBox.
My current code at the moment is:
<%= link_to image_tag #user.image_url(:medium), :style => "width:717px;height:409px" %>
This links to the Users profile page, which is what I don't want.
Please help :)
Use brackets for nesting methods
<%= link_to image_tag( #user.image_url(:medium), :style => "width:717px;height:409px"), #user.image_url %>

Resources