我有条目带属性的模型卡路里和日期。我也有用户模型属性expected_calories。当我显示条目列表时,如果每天的卡路里总和大于expected_calories,则应该显示为红色,否则应为绿色。如何根据Rails中它的属性值更改记录的颜色?
entries_controller.rb
def index
@entries = Entry.where(user_id: current_user.id)
@user = current_user
if @user.role == 'admin'
@entries = Entry.all
end
末
index.html.slim
h1 Listing entries
table.table
thead
tr
th Date
th Time
th Content
th Cal
th User
th
th
th
tbody
- if can? :manage, Entry
- @entries.each do |entry|
tr
td = entry.date
td = entry.time.strftime("%H:%M")
td = entry.content
td = entry.cal
td = entry.user.role
td = link_to 'Show', entry
td = link_to 'Edit', edit_entry_path(entry)
td = link_to 'Destroy', entry, data: { confirm: 'Are you sure?' }, method: :delete
br
= link_to 'New Entry', new_entry_path
你能分享你到目前为止有什么? (特别是在视图部分) –
我已添加。但仍然不知道如何制作。 –