简体   繁体   中英

Ruby on Rails - Conditional table coloring in erb template file

<% @s2_pmt_transact.each do |s2_pmt_transact| %>
<% if (s2_pmt_transact.key = 7) %>
<tr style="background-color:#EAEAEA">
<% else %>
<tr style="background-color:#FFFFFF">
<% end %>

I would like the cell coloring to be based on value of key as in the code above. But all values of key (7 and above) are printed just fine - but color coding seems to be just #EAEAEA for all of them. How can I enforce conditional coloring?

Your conditional is wrong, needs to use == not = which is an assignment and explains why all are grey.

<% @s2_pmt_transact.each do |s2_pmt_transact| %>
 <% if s2_pmt_transact.key == 7 %>
  <tr style="background-color:#EAEAEA">
 <% else %>
  <tr style="background-color:#FFFFFF">
 <% end %>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM