In my model I have two classes, a donation class and a good class. A donation can have mulitple goods, so I set up a has_many relationship between the two objects.
class Donation < ActiveRecord::Base has_many :goods
class Good < ActiveRecord::Base belongs_to :donation
In my view (donation/new.html.erb), I add goods to a donation the following way;
<input id="donation__goods_item" name="donation[goods][1][item]" style="width: 100%;" maxlength="20" type="text"> <textarea id="donation_goods_description" name="donation[goods][1] [description]" style="width: 100%;" rows="3"></textarea> <input id="donation__goods_item" name="donation[goods][2][item]" style="width: 100%;" maxlength="20" type="text"> <textarea id="donation_goods_description" name="donation[goods][2] [description]" style="width: 100%;" rows="3"></textarea> <input id="donation__goods_item" name="donation[goods][2][item]" style="width: 100%;" maxlength="20" type="text"> <textarea id="donation_goods_description" name="donation[goods][2] [description]" style="width: 100%;" rows="3"></textarea>
This will create the following @params after submitting the form.
{"commit"=>"Submit", "authenticity_token"=>"727eec63cfc5b40ee935c0d3478476661893b52c", "donation"=>{"goods"=> {"1"=> {"item"=>"books", "description"=>"Children books"}, "2"=> {"item"=>"shoes", "description"=>"Leather Boots"}, "3"=> {"item"=>"blankets", "description"=>"wool blankets"},}}}
The donation controller:
class DonationsController < ApplicationController def create @donation = Donation.new(params[:donation]) ....
So, I was hoping that ruby on rails automatically generates the goods and adds them to the donation object, but when create is called, i get the following error on the first line;
ActiveRecord::AssociationTypeMismatch in DonationsController#create
Good expected, got Array
Any help is appreciated.