How to Change Font Color based on Conditional?

老子叫甜甜 提交于 2019-12-08 05:06:36

问题


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:


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


来源:https://stackoverflow.com/questions/29189726/how-to-change-font-color-based-on-conditional

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!