views:

296

answers:

4

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'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.

+1  A: 

There are different ways, but I think that you're using the best one.

Ben
+3  A: 

Another option is this:

<%= link_to content_tag(:span, 'New car'), {:action => "new"}, :class=>"button" %>

docs

ryeguy
Nice, this works really well. You can even add little icons to the button by doing:<%= link_to content_tag(:span, 'New Car' + image_tag("car_add.png")), {:action => "new"}, :class=>"button" %>
Zachary
+1  A: 

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 :)

neutrino
+1  A: 

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

Jimmy Baker