views:

228

answers:

1

I'm trying to set up a date of birth helper in my Rails app (2.3.5). At present it is like so.

<%= f.date_select :date_of_birth, :start_year => Time.now.year - 110, :end_year => Time.now.year %>

This generates a perfectly functional set of date fields that work just fine but....

They default to today's date which is not ideal for a date of birth field (I'm not sure what is but unless you're running a neonatal unit today's date seems less than ideal). I want it to read Jan 1 2010 instead (or 2011 or whatever year it happens to be). Using the :default option has proven unsuccessful. I've tried many possibilities including;

<%= f.date_select :date_of_birth, :default => {:year => Time.now.year, :month => 'Jan', :day => 1}, :start_year => Time.now.year - 110, :end_year => Time.now.year %>

and

<%= f.date_select :date_of_birth, :default => Time.local(2010,'Jan',1), :start_year => Time.now.year - 110, :end_year => Time.now.year %>

None of this changes the behaviour of the first example. Does the default option actually work as described? It seems that this should be a fairly straightforward thing to do.

Ta.

+3  A: 

I think that easiest way is to assign it in controller, for example for new action:

@person = Person.new(:date_of_birth => "2010-01-01".to_date)

Then in your view you will get correct date.

EDIT:
If you want to assign default value on model level, you can try do it with plugin. However I haven't try it.

klew
I guess that'll do the trick (although I went with @person = Person.new(:date_of_birth => "#{Time.now.year}-01-01".to_date)). Seems like an awkward way of achieving this though - would seem much tidier in the view with the date_select helper. I'd prefer to keep my controller as clean as possible (partly because I have real difficulty debugging controller problems as it is, while view problems are usually more straightforward.
brad
But I think it should be in controller or even in model as a default value. I added link to plugin that you may be interested.
klew