public
Last active

Did you know you can pass classes into Rails tag helpers as Arrays?

  • Download Gist
gistfile1.txt
1 2 3 4 5 6 7 8 9 10 11
# Let's say you've programatically built up an array of classes for a HTML element:
my_classes = [:a, :b, 'c']
 
# Don't do this, join(" ") is for chumps:
<% content_tag(:p, "Foo", :class => my_classes.join(" ")) %>
 
# Winners do this:
<% content_tag(:p, "Foo", :class => my_classes) %>
# => <p class="a b c">Foo</p>
<% link_to("Foo", foos_path, :class => my_classes) %>
# => <a href="/foos" class="a b c">Foo</a>

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.