Aaron Dufall
Aaron Dufall

Reputation: 1177

setting default year and month to nil with date select instead of 1

I'm using the below line of code for the date select in a form_for

<%= f.date_select :scheduled, {:prompt => {:day => 'Day', :month => 'Month', :year => 'Year'}}, :class => 'date-field', :include_blank => true %>

the date is optional in the form that it is being used. When the form is submitted the year and month field are defaulted to 1, which the api doc's explain will happen when a field is left blank.

below is the hash

"todo"=>{"name"=>"date tezt", "scheduled(1i)"=>"1", "scheduled(2i)"=>"1", "scheduled(3i)"=>"1", "scheduled(4i)"=>"", "scheduled(5i)"=>"", "assigned_to_id"=>""}, "commit"=>"Create To-do", "action"=>"create", "controller"=>"todos", "project_id"=>"1", "todolist_id"=>"1"}

How do you override this so that if nothing is entered the hash generated contains nil, for the year and month?

Upvotes: 2

Views: 709

Answers (2)

Aaron Dufall
Aaron Dufall

Reputation: 1177

The issues was with how date_select and time_select play together.

I needed to add this option to time_select

:ignore_date => true 

full code below

<%= form.date_select :scheduled, {:prompt => true}, :class => 'date-field'  %>
<%= form.time_select :scheduled,  {:minute_step => 15, :ignore_date => true, :ampm => true, :prompt => {:hour => 'Hour', :minute => 'Minutes'}}, :class => 'date-field' %>

Upvotes: 2

Salil
Salil

Reputation: 47512

Add , :include_blank => true Ref date_select

<%= f.date_select :scheduled, {:prompt => {:day => 'Day', :month => 'Month',
    :year => 'Year'}},  :class => 'date-field', :include_blank => true %>

Upvotes: 0

Related Questions