Embed HTML in link_to body in Rails

What is the best way to embed embedded HTML in the body of a link generated using the link_to method?

Basically I want the following:

<a href="##">This is a <strong>link</strong></a> 

I tried to do this as suggested in the Rails and <span> tag , but with no luck. My code is as follows:

item_helper.rb

 def picture_filter #...Some other code up here text = "Show items with " + content_tag(:strong, 'pictures') link_to text, {:pics => true}, :class => 'highlight' end 

item_view.html.erb

  #... <%=raw picture_filter %> #... 
+57
ruby-on-rails link-to
Mar 15 2018-11-11T00:
source share
5 answers

Try it like this.

 <%= link_to(raw("a <strong>strong</strong> link"),{:pics => true},{ :class => 'highlight'}) %> 
+96
Mar 15 2018-11-21T00:
source share
 = link_to "http://www.example.com" do <strong>strong</strong> 
+46
Mar 15 '11 at 21:14
source share

As of 2016, I prefer this method.

 <%= link_to url: my_path do %> This is a <strong>ape</strong> <% end %> 
+13
May 09 '16 at 11:10
source share

you can use html_safe

 <%= link_to ("<i class='someIcon'></i> Link").html_safe %> 
+10
Apr 23 '14 at 6:49
source share

Not sure if this is the best way.

But I was very successful in putting a lot of view helpers in the call to content_tag.

Also does not hurt to call .html_safe

 link_to(content_tag(:span, "Show yada " + content_tag(:strong, "Pictures")), {:pics => true}) 
+4
Mar 15 2018-11-21T00:
source share



All Articles