0

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?

2 Answers 2

4

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>
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! I just forgot about classes :P
0

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)}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.