New-ish to Rails… I’ve created a helper to format race names & their dates. I need to pass :id => "current-race" if a condition is present (basically if event is happening now). How could I go about this?
def formatted_race_dates(race)
link_to (race.homepage) do
raw("<strong>#{race.name}</strong> <em>#{race_dates_as_string(race)}</em>")
end
end
now when race.start_date < Date.today && race.end_date > Date.today I’d like to add id="current-race" to the link.
I would normally set up an if/else condition and format it two ways. But seems there must be a Ruby trick I don’t know to simplify something as common adding of a id/class to one link_to in a list? Even without the condition I’m not quite sure where/how to add :id => "current-race".
So many Ruby/Rails tricks I don’t know…everything helps!
The
link_tomethod takes options for this very reason:You can even add conditions to trigger it selectively:
You can even collapse this if you have a method for Race that indicates if it’s current:
This is easily implemented in your model and can be used in other places:
Having it in the model makes it significantly easier to test.