Zachary Wright
Zachary Wright

Reputation: 24070

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

Upvotes: 20

Views: 28119

Answers (3)

alex.zherdev
alex.zherdev

Reputation: 24164

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

Upvotes: 7

ryeguy
ryeguy

Reputation: 66851

Another option is this:

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

docs

Upvotes: 39

Jimmy Baker
Jimmy Baker

Reputation: 3255

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

Upvotes: 7

Related Questions