I''ve seen some similar questions to this but I cannot find the answer. I have a simple form that is supposed to save some values into the database. However, when I call save, the record is saved with null values instead of the values provided in the form. As you can see by the following console output, the parameters are being posted properly, but for some reason they are not being entered into the database. I''m not doing any validations or anything else. I''m also not receiving any errors. What''s the deal? Processing MotorcyclesController#create (for 127.0.0.1 at 2009-07-09 22:27:34) [POST] Parameters: {"model"=>"asdfsd", "cc"=>"1234", "commit"=>"Save", "authenticity_token"=>"ed86db52751e22006b4af4050daa88d8b722685a", "make"=>"dfad", "year"=>"1234"} Motorcycle Create (0.3ms) INSERT INTO "motorcycles" ("model", "cc", "created_at", "updated_at", "make", "year") VALUES(NULL, NULL, ''2009-07-10 05:27:34'', ''2009-07-10 05:27:34'', NULL, NULL) Rendering template within layouts/application Rendering motorcycles/create
Quoting Whiplash <danapellerin-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>:> > I''ve seen some similar questions to this but I cannot find the answer. > I have a simple form that is supposed to save some values into the > database. However, when I call save, the record is saved with null > values instead of the values provided in the form. > > As you can see by the following console output, the parameters are > being posted properly, but for some reason they are not being entered > into the database. I''m not doing any validations or anything else. I''m > also not receiving any errors. > > What''s the deal? >What about attr_protected or attr_accessible in the model? HTH, Jeffrey
On Jul 10, 6:56 am, Whiplash <danapelle...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> As you can see by the following console output, the parameters are > being posted properly, but for some reason they are not being entered > into the database. I''m not doing any validations or anything else. I''m > also not receiving any errors. > > What''s the deal? >How about showing the code that isn''t working ? Fred> Processing MotorcyclesController#create (for 127.0.0.1 at 2009-07-09 > 22:27:34) [POST] > Parameters: {"model"=>"asdfsd", "cc"=>"1234", "commit"=>"Save", > "authenticity_token"=>"ed86db52751e22006b4af4050daa88d8b722685a", > "make"=>"dfad", "year"=>"1234"} > Motorcycle Create (0.3ms) INSERT INTO "motorcycles" ("model", > "cc", "created_at", "updated_at", "make", "year") VALUES(NULL, NULL, > ''2009-07-10 05:27:34'', ''2009-07-10 05:27:34'', NULL, NULL) > Rendering template within layouts/application > Rendering motorcycles/create
Alright I figured out how to get it to work, but I''m still not sure why. First off, I was using the form_tag method of creating forms. Then in my controller I was doing @motorycle = Motorcycle.new(params[:motorcycle]) @motorcycle.save This did not work. What I''ve done now is change the controller to: @motorcycle= Motorcycle.new @motorcycle.make = params[''make''] @motorcycle.model = params[''model''] @motorcycle.year = params[''year''] @motorcycle.save And that works! So my question becoms, why do I have to manually assign the parameters when using form_tag, and I don''t when using form_for? Thanks Dana
On Jul 10, 4:46 pm, Whiplash <danapelle...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> @motorcycle= Motorcycle.new > @motorcycle.make = params[''make''] > @motorcycle.model = params[''model''] > @motorcycle.year = params[''year''] > @motorcycle.save > > And that works! > > So my question becoms, why do I have to manually assign the parameters > when using form_tag, and I don''t when using form_for? >Because form_for by default stuck your parameters in params [:motorcycle], but the fields you created in the other case where at the top level Fred> Thanks > > Dana
Thanks Fred, starting to understand now. :-) Dana On Jul 10, 8:59 am, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Jul 10, 4:46 pm, Whiplash <danapelle...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > @motorcycle= Motorcycle.new > > @motorcycle.make = params[''make''] > > @motorcycle.model = params[''model''] > > @motorcycle.year = params[''year''] > > @motorcycle.save > > > And that works! > > > So my question becoms, why do I have to manually assign the parameters > > when using form_tag, and I don''t when using form_for? > > Because form_for by default stuck your parameters in params > [:motorcycle], but the fields you created in the other case where at > the top level > > Fred > > > Thanks > > > Dana