Problems with time_select and :include_blank option


#1

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:




00 01 02 03 ... 21 22 23 : 00 15 30 45

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.


#2

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

On Mar 11, 2:20 am, “Rodrigo Tassinari” removed_email_address@domain.invalid


#3

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.


#4

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

On Mar 15, 12:16 pm, “Rodrigo Tassinari” removed_email_address@domain.invalid