简体   繁体   中英

construct link_to in Rails 3.2.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)

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>"), "#" %>

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>"),"#" %>

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM