开发者

Adding span tags to ruby link?

开发者 https://www.devze.com 2022-12-15 15:44 出处:网络
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\") %>

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 %>
0

精彩评论

暂无评论...
验证码 换一张
取 消