I have the following code in Ruby on Rails 3. What is the preferred way to set the class (or id) to the value of a variable, and output it?
<%= tag "td", :class => @priority_level %><%= @priority_level %></td>
Outputs:
<td class="normal">normal</td>
Thanks.
The content_tag helper is sometimes visually more appealing than intermixing Erb and HTML:
<%= content_tag(:td, @priority_level, :class => @priority_level) %>
There are also other templating options out there besides the default Erb.
Here's the equivalent in Haml :
%td{:class => @priority_level}= @priority_level
and Mustache:
<td class="{{priority_level}}">{{priority_level}}</td>
I think that both are easier on the eyes than Erb. If you're stuck in Erb-land, organizing your code into helper methods and partials as much as possible is a good way to keep Erb templates visually manageable.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With