How to set default date in date_select helper in Rails

I am trying to set the helper's birth date in my Rails application (2.3.5). This is currently the case.

<%= 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 fine, but ....

By default, they have today's date, which is not ideal for the date of birth (I'm not sure what it is, but if you are not working with a newborn unit, today the date seems less ideal). I want him to read January 1, 2010 (or 2011, or any other year when this happens). Using the: default option failed. I have tried many features, 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 behavior of the first example. Does the default option really work as described? It seems like this should be a fairly simple task.

That one.

+4
source share
1 answer

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

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

Then, in your opinion, you will receive the correct date.

EDIT:
If you want to assign a default value at the model level, you can try to do this using the plugin . However, I have not tried.

+10
source

Source: https://habr.com/ru/post/1305837/


All Articles