Set default value for date field in rails 4

I have several forms in my application where, when using date_select and I just changed the use of date_field (which uses HTML5 form elements), the only problem I encountered is that date_select will default to today's date, which was perfect for us, but date_field does dd/mm/yyyy instead.

I am having problems getting people to blindly submit a form and cause validation errors.

Is it possible to use date_field by default?

+8
ruby-on-rails
source share
8 answers

If you installed Rails4 through gem, it still has an error.

https://github.com/rails/rails/commits/master/actionview/lib/action_view/helpers/tags/datetime_field.rb

Correct above and you can set a default value like this

 <%= f.date_field :birthday, :value => Time.now.strftime('%Y-%m-%d') %> 
+15
source share

You can set the default date_field as follows

 date_field("user", "born_on", value: Time.now.to_s(:db)) 
+2
source share

This will work correctly:

 <%= f.date_field :birthday, :value => Time.now.strftime("%Y-%m-%d") %> 
+2
source share

None of these options worked for me trying to use date_field_tag .

That the work conveyed a meaning similar to this:

date_field_tag(:my_thing_end_date, :end_date, value: Date.current.strftime('%Y-%m-%d')

From documents ( http://apidock.com/rails/v4.0.2/ActionView/Helpers/FormHelper/date_field )

The default value is generated when you try to call "to_date" on the value of objects, which causes it to behave as expected for instances of DateTime and ActiveSupport :: TimeWithZone.

So, passing the date as a string to be entered by the user seems to be the solution.

Also, according to this good information on dates and time zones, Date.current preferable to Time.today : https://robots.thoughtbot.com/its-about-time-zones .

+2
source share
 f.date_field :birthday, :default=>Date.today.strftime("%d/%m/%Y") 
+1
source share

Using a Time object for a date field did not work for me, I had to use a Date object.

  • I used placeholder for browsers like Safari to lead them to the correct formatting if it displays as a text box.
  • Also, if a value already exists (for example, when you are in the #edit action), it should use that value instead

The resulting view code for Rails 4:

 <%= f.date_field :birthday, placeholder: "YYYY-MM-DD", value: (f.object.birthday || Date.today.to_s(:db)) %> 
0
source share

In Rails, you can also do the following: '<% = f.date_field: birthday ,: value => Date.today%>

0
source share

Setting the value: as many of these answers indicate, will overwrite the value that is passed for any record being edited. This is not a problem if the form is intended only for new records, but a mistake if you want to edit records in the form. One way to do this is to explicitly capture an existing object, as Michael suggests.

The solution I'm currently using is in the controller, which sets the value today when it creates a new record. Assuming your model is Person, and the value you want to use by default is start_date:

 def new @person = Person.new(start_date: Date.current) end 

Then you can simply use the value in the form without any additional fuss.

 f.date_field :start_date 
0
source share

All Articles