1

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,

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

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

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

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

About
Me

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)

3 Answers3

4

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, '#' %>

or

<%= link_to raw("About <span> Me</span>"), "#" %>
kclair
  • 2,124
  • 1
  • 14
  • 18
  • 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 –  Mar 13 '12 at 00: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 –  Mar 14 '12 at 01:03
  • 2
    http://stackoverflow.com/questions/4251284/raw-vs-html-safe-vs-h-to-unescape-html – Ed Jones Mar 14 '12 at 11:13
1

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
    end
  end

And use it as follows:

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

If not,

<%= link_to raw("About <span>Me</span>"),"#" %>
Fareesh Vijayarangam
  • 5,037
  • 4
  • 23
  • 18
0

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")
end

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

link_to '#' { content_tag(:span, "About me") }
JohnColvin
  • 2,489
  • 1
  • 14
  • 8