Hi there, Suppose I submit an object called "task" from a form with... task[enddate(1i)] -> 2011 task[enddate(2i)] -> 3 task[enddate(3i)] -> 1 ...how do I set the value ''params[:task][:enddate]'' to ''nil'' in the controller? I tried... params[:task][:enddate] = nil ...but that doesn''t seem to "empty" the attribute ''enddate''. What am I getting wrong? Thanks for your help! Tom -- Posted via http://www.ruby-forum.com/. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Colin Law
2010-Apr-22 14:43 UTC
Re: How to: Set a datetime params-value to nil in the controller
On 22 April 2010 15:22, Tom Ha <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Hi there, > > Suppose I submit an object called "task" from a form with... > > task[enddate(1i)] -> 2011 > task[enddate(2i)] -> 3 > task[enddate(3i)] -> 1 > > ...how do I set the value ''params[:task][:enddate]'' to ''nil'' in the > controller? > > I tried... > > params[:task][:enddate] = nil > > ...but that doesn''t seem to "empty" the attribute ''enddate''. What am I > getting wrong?It is difficult to see how that line could not set params[:task][:enddate] to nil, assuming it did not generate an error such as it would if params[:task] were nil. Are you sure it is executing that line of code? You could break in at that point using ruby-debug and check. See the rails guide on debugging for how to do it (google rails guides). Colin -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Michael Pavling
2010-Apr-22 14:51 UTC
Re: How to: Set a datetime params-value to nil in the controller
On 22 April 2010 15:22, Tom Ha <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> ...how do I set the value ''params[:task][:enddate]'' to ''nil'' in the > controller? > > I tried... > > params[:task][:enddate] = nil > > ...but that doesn''t seem to "empty" the attribute ''enddate''. What am I > getting wrong?If it doesn''t "empty" (whatever you mean by that) the :enddate entry, what *does* it do? I''ve just had a play in the console, and it all seems to work as expected:>> params = {:task => {:enddate => Date.today.to_s}}=> {:task=>{:enddate=>"2010-04-22"}}>> params[:task][:enddate]=> "2010-04-22">> params[:task][:enddate] = nil=> nil>> params=> {:task=>{:enddate=>nil}}>> params[:task][:enddate]=> nil -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Tom Ha
2010-Apr-22 16:58 UTC
Re: How to: Set a datetime params-value to nil in the controller
Thanks for your inputs. Unfortunately, I can add that: 1. Yes, the code definitely GETS executed 2. No, params[:task] is definitely NOT nil 2. Other values of the Task object in params CAN be set to nil, for example... params[:task][:name] = nil ...works correctly. Could it be that "...[:enddate]" is not sufficient in... params[:task][:enddate] ...because the date gets submitted by a date_select? When I check what values get passed to params, I see... task[enddate(1i)] -> 2011 task[enddate(2i)] -> 3 task[enddate(3i)] -> 1 Could it be that the problem stems from the "(1i), (2i), (3i)" part? Any ideas? -- Posted via http://www.ruby-forum.com/. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Colin Law
2010-Apr-22 18:53 UTC
Re: Re: How to: Set a datetime params-value to nil in the controller
On 22 April 2010 17:58, Tom Ha <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Thanks for your inputs. > > Unfortunately, I can add that: > > 1. Yes, the code definitely GETS executed > 2. No, params[:task] is definitely NOT nil > 2. Other values of the Task object in params CAN be set to nil, for > example... > > params[:task][:name] = nil > > ...works correctly. > > Could it be that "...[:enddate]" is not sufficient in... > > params[:task][:enddate] > > ...because the date gets submitted by a date_select? When I check what > values get passed to params, I see... > > task[enddate(1i)] -> 2011 > task[enddate(2i)] -> 3 > task[enddate(3i)] -> 1 > > Could it be that the problem stems from the "(1i), (2i), (3i)" part?Well yes, quite likely. So when you said that params[:task][:enddate] was not getting set to nil, did you actually mean that the date in the database when you save it is not cleared? It looks like it might be an idea to clear the three values in params that are used for setting the date. If you are not sure of the format then use ruby-debug to break in, then you can inspect params and see exactly what is there so you can clear it. Colin -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Frederick Cheung
2010-Apr-22 20:20 UTC
Re: How to: Set a datetime params-value to nil in the controller
On Apr 22, 5:58 pm, Tom Ha <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> ...because the date gets submitted by a date_select? When I check what > values get passed to params, I see... > > task[enddate(1i)] -> 2011 > task[enddate(2i)] -> 3 > task[enddate(3i)] -> 1 > > Could it be that the problem stems from the "(1i), (2i), (3i)" part? >> Any ideas?Unless i''ve misunderstood what you are attempting, you need to clear out the 3 parts of end date. You can''t remove params[:task][:enddate] because it doesn''t exist - when activerecord sees parameters with the appropriate naming convention it gathers them together to create an instance of Time and assigns that to the task''s enddate. If you want to set enddate to nil it would probably be enough to remove those 3 parts and then set params[:task][:enddate] to nil (although why jump through all those hoops and not just do some_task.enddate = nil ?) Fred> -- > Posted viahttp://www.ruby-forum.com/. > > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. > For more options, visit this group athttp://groups.google.com/group/rubyonrails-talk?hl=en.-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Tom Ha
2010-Apr-23 13:30 UTC
Re: How to: Set a datetime params-value to nil in the controller
Thanks for the explanations - they helped! -- Posted via http://www.ruby-forum.com/. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.