Kombo
Kombo

Reputation: 2381

Rails 3 - link_to assign multiple classes

I have a loop that links to every item in a collection:

<% current_user.projects.all.each do |p| %>
    <%= link_to p.name, project_path(p), :class => current_class?(project_path(p)), :id => p.theme %>
  <% end %>

Right now, I'm assigning a class for the anchor in addition to an id so I can apply some CSS. Really though, it would make more sense for me to have this as a double classed anchor. Is there a way to assign two classes to the same object with the link_to helper?

Upvotes: 4

Views: 8984

Answers (1)

Jeremy Roman
Jeremy Roman

Reputation: 16355

Just separate them by spaces, as you would in ordinary HTML.

<%= link_to p.name, project_path(p), :class => "class1 class2 class3", :id => p.theme %>

You can generate the string however you want. If you have the classes you want as an array you could use some_classes.join(" ") to combine them.

Upvotes: 15

Related Questions