Forum: Ruby on Rails Problems with time_select and :include_blank option

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
E574e92e9632755338065d39129fd49a?d=identicon&s=25 Rodrigo Tassinari (Guest)
on 2007-03-10 16:22
(Received via mailing list)
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.
B4f6108ff75c1407f8115a7b962bc850?d=identicon&s=25 AaronT (Guest)
on 2007-03-14 04:26
(Received via mailing list)
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" <rodr...@pittlandia.net>
E574e92e9632755338065d39129fd49a?d=identicon&s=25 Rodrigo Tassinari (Guest)
on 2007-03-15 02:18
(Received via mailing list)
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.
B4f6108ff75c1407f8115a7b962bc850?d=identicon&s=25 AaronT (Guest)
on 2007-03-16 10:13
(Received via mailing list)
Yes it automatically fills in the value if not null
luckly no extra hacking was needed

On Mar 15, 12:16 pm, "Rodrigo Tassinari" <rodr...@pittlandia.net>
This topic is locked and can not be replied to.