Better cleaner way to add ID/Class to Rails link_to?

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!

Answers


The link_to method takes options for this very reason:

link_to(race.homepage, :id => 'current-race') do ...

You can even add conditions to trigger it selectively:

link_to(race.homepage, :id => (race.start_date < Date.today && race.end_date > Date.today) ? 'current-race' : nil) do ...

You can even collapse this if you have a method for Race that indicates if it's current:

link_to(race.homepage, :id => race.current? ? 'current-race' : nil) do ...

This is easily implemented in your model and can be used in other places:

def current?
  self.start_date < Date.today && self.end_date > Date.today
end

Having it in the model makes it significantly easier to test.


Need Your Help

Text is not coming properly with Custom LabelField in Blackberry

blackberry custom-fields labelfield

I am implementing Custom LabelField in my app. It is working fine with small fonts, when I increase the font size it will not show properly. Here you can see it in this image.

Reset rdp session with psexec

session rdp

We have domain users that use a remote instance of an app through RDP. The app is quite buggy, and their session needs reset frequently. I'd like to build a script that uses psexec from the client to

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.