How to Change Font Color based on Conditional?

2019-05-29 22:46发布

When a User checks off :good in the _form

    <%= f.text_field :result_value %>
    <%= f.date_select :date_value %>
    <%= f.check_box :good %>

how can we render the font color in the index of that result (:result_value & :date_value) to green?

result.good below pulls up true or false in the index. I'll take that out once we represent true or false with colors: true = green, false = red (default color).

        <% averaged.results.each do |result| %>
          <li>
            <%= result.result_value %>
            <%= result.date_value.strftime("%b %Y") %>
            <%= result.good %>
          </li>
        <% end %>

.

class Result < ActiveRecord::Base
belongs_to :user
  belongs_to :quantified
  has_many :comments, as: :commentable
  default_scope { order('date_value DESC') }
  scope :good, -> { where(good: true) }
  scope :good_count, -> { good.count }
end

Thank you so much for your help.

1条回答
三岁会撩人
2楼-- · 2019-05-29 23:19

You could add a class to the LI based on the output?

<% if result.good == true %>
    <li class="green">
<% else %>
    <li>
<% end %>

Then in your css just set a colour for that class?

.green {
    color: green;
}
查看更多
登录 后发表回答