How do you add span tags to the link below in ruby?
<%= link_to (l(:button_show), {:action => 'show', :path => to_path_param(@path)}, :class => "button") %>
I would like to add the span tags to the link like this:
<a href="/show" class="button"><span>Show</span></a开发者_StackOverflow中文版>
Kevin's answer will work fine. I far prefer his second option (using the block) to putting an interpolated string in as an argument. I might actually prefer this:
<%= link_to content_tag(:span, l(:button_show)), {:action => 'show', :path => to_path_param(@path)}, :class => "button" -%>
content_tag
simply returns an HTML string. Its first argument is the name of the tag. The second is the tag content. (There's an alternative block usage, but that would just complicate things here.)
Totally possible. The first parameter to link_to
is just an arbitrary string, so you could do this:
<%= link_to("<span>#{l(:button_show)}</span>",
{:action => 'show', :path => to_path_param(@path)}, :class => "button") %>
Since link_to
can take a block for the name, though, a more readable way might be to do this:
<% link_to({:action => 'show', :path => to_path_param(@path)}, :class => "button") do %>
<span><%= l(:button_show) %></span>
<% end %>
精彩评论