74

What is the best way to go about getting embedded HTML in the body of a link generated with the link_to method?

I basically want the following:

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

I have been trying to go about this as suggested in Rails and the <span> tag but with no luck. My code looks like the following:

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 %>
 #...
Community
  • 1
  • 1
Ryan
  • 2,460
  • 1
  • 21
  • 22

5 Answers5

114

Try it this way

<%= link_to(raw("a <strong>strong</strong> link"),{:pics => true},{ :class => 'highlight'})  %>
Valdis
  • 3,170
  • 2
  • 18
  • 24
  • Applied to my example, the call became: link_to raw(text), {:pics => true}, :class => 'highlight' – Ryan Mar 15 '11 at 21:28
62
= link_to "http://www.example.com" do
   <strong>strong</strong>
mark
  • 10,316
  • 6
  • 37
  • 58
32

As of 2016 I prefer this method.

<%= link_to my_path do %>
    This is a <strong>ape</strong>
<% end %>
BenKoshy
  • 33,477
  • 14
  • 111
  • 80
GorillaApe
  • 3,611
  • 10
  • 63
  • 106
17

you can use html_safe

<%= link_to ("<i class='someIcon'></i> Link").html_safe %>
Syed
  • 15,657
  • 13
  • 120
  • 154
4

Not sure if this is the best way.

But I have been very successful in staking alot of the view helpers inside the content_tag call.

It also might not hurt to call a .html_safe

link_to(content_tag(:span, "Show yada " + content_tag(:strong, "Pictures")), {:pics => true})