What is the best way to add classes to table cells in rails based on differing criteria for each cell?
For example...I have a date cell: If the date is coming up on 10 days from that date, need to add a "upcoming-due" class. Or if the date is past today, add a "past-due" class.
On another cell, based on a string, it needs to have a class "on" or "off".
I'm not sure how to do this, but I do know I probably shouldn't have a bunch of logic in the views...
Please advise.
You could accomplish it with something like this:
class="#{(@date - Time.now <= 10.days) ? upcoming_due : '' %>"
However, as you mentioned, it's not good to have a lot of logic in the view. If you find yourself repeating the same code, you can add helper methods which look like this:
def upcoming(date)
(date - Time.now <= 10.days) ? 'upcoming_due' : ''
end
def past(date)
(date - Time.now <= 10.days) ? 'past_due' : ''
end
In your view, you'd have something like this:
class="#{ upcoming(@date).presence || past(@date).presence || '' }"