Hi,
I''m really excited about getting the create() method working on a
nested hash. The Rails book make it seem like I''m close to doing the
right thing but I think something very small is not quite right. The
error at the below isn''t giving any hints that I understand.
I have a form that produces a nested params hash and sends it to my
add_to_cart controller method. Params contains a product variation_id
and a list of chosen options (eg. color, size, etc).
Any help greatly appreciated.
Thanks,
Peter
params {
"commit"=>"Add To Cart",
"variation_id"=>"1",
"cart_item_options"=>{"6"=>{"option_id"=>"74"},
"7"=>{"option_id"=>"97"},
"5"=>{"option_id"=>"47"}}
}
## CONTROLLER ##
def add_to_cart
CartItem.create(params}
end
## MODELS ##
class CartItem < ActiveRecord::Base
belongs_to :variation
has_many :cart_item_options
end
class CartItemOption < ActiveRecord::Base
belongs_to :cart_item
end
## TABLES ##
CREATE TABLE cart_items (
id SERIAL,
variation_id INTEGER UNSIGNED NOT NULL
);
CREATE TABLE cart_item_options (
id SERIAL,
cart_item_id INTEGER UNSIGNED NOT NULL,
optional_id INTEGER UNSIGNED NOT NULL
);
## ERROR ##
CartItemOptionalVariation expected, got Array
RAILS_ROOT: script/../config/..
Application Trace | Framework Trace | Full Trace
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/associations/association_collection.rb:128:in
`raise_on_type_mismatch''
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/associations/association_collection.rb:114:in
`replace''
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/associations/association_collection.rb:114:in
`each''
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/associations/association_collection.rb:114:in
`replace''
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/associations.rb:770:in
`cart_item_optional_variations=''
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/associations.rb:762:in
`cart_item_optional_variations=''
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/base.rb:1337:in
`send''
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/base.rb:1337:in
`attributes=''
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/base.rb:1336:in
`each''
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/base.rb:1336:in
`attributes=''
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/base.rb:1188:in
`initialize_without_callbacks''
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/callbacks.rb:236:in
`initialize''
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/base.rb:449:in
`new''
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/base.rb:449:in
`create''
./script/../config/../app/controllers/store_controller.rb:79:in
`add_to_cart''
Peter Michaux
2006-Feb-27 21:01 UTC
[Rails] Re: Nested hash from form params into create()?
Sorry, I posted an error from a bigger version of the problem. Here is the smaller version''s error message. It seems like create() doesn''t like having any extra stuff sent to it. However, I don''t think I should have to remove commit from the params hash for it to work as nicely as the rails book implies. Thanks, Peter undefined method `commit='' for #<CartItem:0x27239bc> RAILS_ROOT: script/../config/.. Application Trace | Framework Trace | Full Trace /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/base.rb:1498:in `method_missing'' /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/base.rb:1337:in `send'' /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/base.rb:1337:in `attributes='' /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/base.rb:1336:in `each'' /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/base.rb:1336:in `attributes='' /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/base.rb:1188:in `initialize_without_callbacks'' /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/callbacks.rb:236:in `initialize'' /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/base.rb:449:in `new'' /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/base.rb:449:in `create'' ./script/../config/../app/controllers/store_controller.rb:79:in `add_to_cart''