Take the 2-minute tour ×

Here my designer change the way to use anchor tag in html template, so I need to change in my rails template too,

if he placed below tag in approved html pages,


I am converting it like this way

<%= link_to "About Me", '#' %>

Now if he placed <a href="#">About <span> Me</a> with span tag in title

       <li><a href="#">About <span>Me</span></a></li>

Here is the output, basically span tag break the line and display in second line


Now I need help to convert this tag with rails 3.2.1 tag.

like <%= link_to "About <span> Me</span>", '#' %>

How can I do that? (I know that will generate error message)

share|improve this question
Where does the span end? That doesn't look like valid HTML. –  JohnColvin Mar 12 '12 at 23:38
@JohnColvin - Just update the answer. Sorry I was missed the ending span –  user1224512 Mar 12 '12 at 23:42
possible duplicate of Embedded HTML in link_to body in Rails –  Michael Durrant Mar 13 '12 at 0:07
add comment

3 Answers

up vote 4 down vote accepted

You need to make sure rails doesn't escape the html tags. You can do this either using html_safe or raw:

<%= link_to "About <span> Me</span>".html_safe, '#' %>


<%= link_to raw("About <span> Me</span>"), "#" %>
share|improve this answer
Note also that there is a comma between the display text and the hyperlink--the original question was missing the comma. –  Ed Jones Mar 12 '12 at 23:56
@EdJones - Good catch, I just change my question, Thanks –  user1224512 Mar 13 '12 at 0:02
@EdJones - Thanks, that's works for me, I have one question Which one is best either html_safe or raw? if we consider best practices, performance, security point of view –  user1224512 Mar 14 '12 at 1:03
@kclair - good link –  Amit Pandya Apr 10 '12 at 22:56
add comment

If wrapping the entire link in a span is alright, create a helper that does the following:

  def spanned_link_to(name,path=nil,options=nil)
    content_tag :span do
      link_to name, path, options

And use it as follows:

<%= spanned_link_to "About Me","#" %>

If not,

share|improve this answer
+1 link_to raw, cool. new to me today –  Michael Durrant Mar 13 '12 at 0:07
add comment

I would have link_to yield to a block. I'm not exactly sure what that looks like in ERB (I use HAML). You can write a link to like this, though:

link_to '#' do
  content_tag(:span, "About me")

or I think you can do a one liner like this:

link_to '#' { content_tag(:span, "About me") }
share|improve this answer
like mine, doesn't address just the 'Me' –  Michael Durrant Mar 13 '12 at 0:05
add comment

Your Answer

