Last Updated: February 25, 2016
·
727
· danbt79

Musing on select inputs

Musings on select inputs. (aka: how Rails is doing it right.)

This is what Rails looks like in the books:
ruby f.date_select :end_date

This is what Rails looks like in production:
ruby f.date_select :end_date, {:include_blank => false, :start_year => Time.zone.now.year, :end_year => (Time.zone.now + 2.years).year, :order => [:month, :day, :year], :prompt => {:month => t(:select_month), :day => t(:select_day), :year => t(:select_year)} }, {:class => 'datetime_picker'}

The second may be long, but it's still just a single line.

It is also:

  • pre-populated with our object's values.

  • ordered as the client requested.

  • styled with a beautiful jQuery date picker.

  • sensitive to the User's timezone.

  • translated into 8 different languages.

This helper method (in the first instance) is clear and easy to use. For beginners, it's simple and almost as easy as just writing a bare html select input.

In the second instance, the same helper method is immensely powerful further down the line as requirements have grown.

None of this complexity has to be used initially, but it's there in plain site if you need it. Rails is restrictive my ass.