Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 978
Kombo Avatar asked Dec 10 '22 07:12

Kombo


1 Answers

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.

like image 184
Jeremy Roman Avatar answered Dec 28 '22 23:12

Jeremy Roman