Rails and the <span> tag
I'm fairly new to Ruby on Rails, and I'm attempting to create some fancy CSS buttons using the "sliding doors" technique. I have it almost working, but I feel like there has to be a better way to handle the tags for a link.
The way I'开发者_Python百科m currently doing it:
<%= link_to '<span>New car</span>', {:action => "new"}, :class=>"button" %>
This isn't terrible, per se, but I would like to know if this is the best way to handle span tags in RoR.
Another option is this:
<%= link_to content_tag(:span, 'New car'), {:action => "new"}, :class=>"button" %>
docs
Or you could be pro and use named routes/resources + Haml. That would make it look like:
%a{ :href => new_car_path }
%span New Car
What you have is fine though..
If you're still curious, here are some ways to rewrite your code:
Use content_tag
:
<%= link_to content_tag("span", "New car"), {:action => "new"}, :class=>"button" %>
Use link_to
with a block:
<%= link_to {:action => "new"}, :class=>"button" do %>
<span>New card</span>
<% end %>
And of course, you can combine the two by putting a content_tag
inside the block, but I'll leave it to the reader as an exercise :)
精彩评论