Problems with time_select and :include_blank option

Hello everyone,

I'm having issues with a "time" field in a model. On the view, I use a time_select helper to generate the select for this field, and since I need this field to be NULL sometimes, I include the option :include_blank => true, like this:

<%= time_select 'support', 'duration', :minute_step => 15, :include_blank => true %>

The problem is that when I use :include_blank, the generated select comes with the hidden fields (of year, month and day) empty, like this:

<input name='support[duration(1i)]' type='hidden' id='support_duration_1i' value='' /> <input name='support[duration(2i)]' type='hidden' id='support_duration_2i' value='' /> <input name='support[duration(3i)]' type='hidden' id='support_duration_3i' value='' /> <select name='support[duration(4i)]' id='support_duration_4i'> <option value=''></option> <option value='00'>00</option> <option value='01'>01</option> <option value='02'>02</option> <option value='03'>03</option> ... <option value='21'>21</option> <option value='22'>22</option> <option value='23'>23</option> </select>   : <select name='support[duration(5i)]' id='support_duration_5i'> <option value=''></option> <option value='00'>00</option> <option value='15'>15</option> <option value='30'>30</option> <option value='45'>45</option> </select>

And so, when I submit the form with the time field filled, I get an ActiveRecord::MultiparameterAssignmentErrors. If I don't set the option :include_blank => true, the hidden fields for year, month and day come filled with the respective current values, which are ignored by MySQL since we're storing them in a time field, not a datetime, and the insert or update goes on fine.

Is this a Rails bug (i'm using version 1.2.2) or am I doing something wrong here?

Thanks, Rodrigo.

I just had the same issue (wasted hours trying to work it out, it shouldnt be this painfull)

What i have ended up doing (it is a bit horrible but it works)

<%= select_time @event.start_time, {:prefix => 'start_time', :time_separator => ' : ', :include_blank => true} %>

Then in the update action before the update_attributes call

    if params[:start_time] && ! params[:start_time][:hour].blank? && ! params[:start_time][:minute].blank?       params[:event][:start_time] = "#{params[:start_time] [:hour]}:#{params[:start_time][:minute]}:00"     else       params[:event][:start_time] = nil     end

I would really like to hear about a cleaner way to do this

Aaron

AaronT,

With this solution, does the time_select field still automatically gets the model field value in edit forms? Or do you need some hacking in the edit action too?

Rodrigo.

Yes it automatically fills in the value if not null luckly no extra hacking was needed