On 28 Aug 2008, at 22:36, billv wrote:
>
> Hi,
>
> I''m entering two dates into a view through the date_select helper,
the
> first relates to a field in the database, the second has been added to
> the model with attr_accessor. When the create action is invoked, it
> fails when trying to create a new object. Here''s the code:
>
> Error
>
> You have a nil object when you didn''t expect it!
> The error occurred while evaluating nil.klass
>
> Parameters
>
> {"commit"=>"Create",
>
"authenticity_token"=>"1b2c46eed03c8154cc26d32fa8b01d6e4caa4490",
> "cto"=>{"end_date(3i)"=>"28",
> "dayoff(1i)"=>"2008",
> "dayoff(2i)"=>"8",
> "dayoff(3i)"=>"28",
> "end_date(1i)"=>"2008",
> "end_date(2i)"=>"8"}}
>
> Any ideas? If I change the date_select to a select_date, the error
> stops, but then I don''t have the data in my model.
>
Long version: This is due to the slightly magic way in which
date_select works. As you can see you get three parameters. The (1i),
(2i) etc.. suffices mean ''this one is the nth parameter to the
constructor and it''s an integer''. Rails then looks at your
model, sees
that dayoff is a date column and knows that it should creating a new
Date, passing the 3 2008,8 and 28 as arguments.
It can''t do this trick with end_date because it has no way of knowing
what kind of object it should be creating.
Short version: use select_date (you''ll have to take the appropriate
parameters and turn them into a date yourself.
Fred> Sorry for the long message, just thought the code would help.
> >
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---