Ich habe Eintrag Modell mit Attribut Kalorien und Datum. Ich habe auch Benutzer Modell mit Attribut expected_calories. Wenn ich die Liste der Einträge anzeigen, sollte es rot sein, wenn die Summe der Kalorien pro Tag mehr als expected_calories ist, sonst sollte grün sein.Wie ändert man die Farbe des Datensatzes entsprechend dem Wert seines Attributs in 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
Ende
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
Können Sie teilen Was hast du bisher? (vor allem auf der Ansicht Teil) –
Ich habe hinzugefügt. Aber ich weiß immer noch nicht, wie ich es machen soll. –