Rodrigo Tassinari
2007-Mar-10 15:20 UTC
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. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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...-9xB6k4dNltl2MFLXGSI/Gg@public.gmane.org>
wrote:> 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.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
Rodrigo Tassinari
2007-Mar-15 01:16 UTC
Re: Problems with time_select and :include_blank option
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. On 14 mar, 00:25, "AaronT" <a...-PT4onWiwnHaHXe+LvDLADg@public.gmane.org> wrote:> 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...-9xB6k4dNltl2MFLXGSI/Gg@public.gmane.org> > wrote: > > > 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.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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...-9xB6k4dNltl2MFLXGSI/Gg@public.gmane.org> wrote:> 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. > > On 14 mar, 00:25, "AaronT" <a...-PT4onWiwnHaHXe+LvDLADg@public.gmane.org> wrote: > > > > > 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...-9xB6k4dNltl2MFLXGSI/Gg@public.gmane.org> > > wrote: > > > > 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.- Hide quoted text - > > - Show quoted text ---~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---