11

When using <%= f.datetime_select :start %> for a "new" object form, how can you set the options to not select a date by default but rather a nil/null value? The field in the related model can optionally be null but by default the datetime_select control doesn't have a null option.

Michael Glenn
  • 1,872
  • 1
  • 19
  • 23

2 Answers2

29

I think you want:

f.datetime_select :start, :include_blank => true

When start is nil, the fields will select the blank option. This also allows users to not enter a date.

See the documentation for date_select for details.

Luke Francl
  • 31,028
  • 18
  • 69
  • 91
0

You'll want to use the :prompt option.

from the Rails docs:

  # Generates a datetime select with a custom prompt. Use :prompt=>true for generic prompts.
  datetime_select("post", "written_on", :prompt => {:day => 'Choose day', :month => 'Choose month', :year => 'Choose year'})
  datetime_select("post", "written_on", :prompt => {:hour => true}) # generic prompt for hours
  datetime_select("post", "written_on", :prompt => true) # generic prompts for all
Mike Breen
  • 2,610
  • 1
  • 19
  • 13
  • Extending the sample didn't seem to work. <%= f.datetime_select :start, :prompt => {:day => 'Choose day', :month => 'Choose month', :year => 'Choose year'} %> Is there a syntax error here? – Michael Glenn Feb 18 '09 at 03:40
  • You could use the vanilla helper: <%= datetime_select :my_object, :start, :prompt => {:day => 'Choose day', :month => 'Choose month', :year => 'Choose year'} %> – Mike Breen Feb 18 '09 at 11:37