Kert
Kert

Reputation: 1604

Rails - using CSS on some condition

I have a requirement to make a app that has a view. In that view I need to check a condition and if it is true, then need to color a table row appropriately. The easiest way to do this is just to use different header in the view. But how to do it if I want to keep all my styling information in CSS?

Upvotes: 0

Views: 1180

Answers (2)

kain
kain

Reputation: 5570

application_helper.rb

def my_color_for(condition)
  if condition
    'white'
  else
    'blue'
  end
end

view.haml

- @array.each do |a|
  %tr{class: my_color_for(a.value)}

Upvotes: 0

mind.blank
mind.blank

Reputation: 4880

If it's just a row you want to colour then you can do it in the view, no need to mess around with headers:

<tr class="<%= "blue" if some_condition %>">
  <td>Your text</td>
</tr>

Or:

<% if some_condition %>
  <tr class="blue">
<% else %>
  <tr class="red">
<% end %>
  <td>Your text</td>
</tr>

Upvotes: 4

Related Questions