I can get the list successfully when create the new item, but when I
press the “create” button, I’ve got the “Brand(#57323960) expected,
got String(#21132310)” AssociationTypeMismatch error
I am not sure why I got the AssociationTypeMismatch error, and how can
I handle this?
I sometimes find these helper methods quite confusing. The key is
what parameters they are sending back to your controller when you hit
the create button.
Check your logs if you’re unsure.
The above line is probably sending
params[:item][:brand] =>
where is the id number for an existing brand.
Another way to check is to look at the generated select tag; it will
probably be <select name=“item[brand]” …> (note how ‘params’ builds
its hash structure off the ‘name’ attribute).
You might want to change it to:
<%= f.collection_select(:brand_id, Brand.find(:all), :id, :name,
{:prompt => “please select one brand”}) %>
It depends on what your ‘create’ controller is doing; I assume you’re
just instantiating a new item object and passing over the field values
from ‘params’. So if your object sees params[:item][:brand_id] you
might get what you want - the will be inserted into the
items.brand_id field. If it sees ‘brand’ as per the original, then
I’m guessing it’s trying to build the assocation via the Item#brand
association method which probably expects a Brand object and not its
id (in string form).
I always have to go back and check the docs. So what I’ve said above
might not be totally right. Get to know how ‘params’ is generated.
Writing stuff test-first on your functional tests will start you
thinking like this too.
Well, thank you very much Daniel your information is quite useful
I checked my code, yes, it is like <select name=“item[brand]” …>
I forgot to mentioned that, I have tried to use "<%=
f.collection_select(:brand_id, Brand.find(:all), :id, :name,
{:prompt => “please select one brand”}) %> "
But, I have got another error “undefined method `brand_id’ for #<Item:
0x6ce3258>”
I assumed maybe there is another place I should take care of? in the
items controller?
Hi I have been following this discussion and I have added a
brand_id:integer field to my items table. The create button now works
and populates the brand column with a number.
I’ve added this to the item show view:
<% for brand in Brand.find(:all, :conditions => {:id => @item.id}) %>
<%= brand.name %>
<% end %>
However some of the brand names are not the name I selected when I
created the entry.
Well, thank you very much Daniel your information is quite useful
I checked my code, yes, it is like <select name=“item[brand]” …>
I forgot to mentioned that, I have tried to use "<%=
f.collection_select(:brand_id, Brand.find(:all), :id, :name,
{:prompt => “please select one brand”}) %> "
But, I have got another error “undefined method `brand_id’ for #<Item:
0x6ce3258>”
Just to make sure: do you have a brand_id integer field in your items
table?
What is the code for your controller - the thing you’re posting to?
However some of the brand names are not the name I selected when I
created the entry.
is this suppose to show the brands for a particular item? The above
can’t do that - you’re selecting brands whose id matches the items id,
the results will be unpredictable at best. @item.brand contains the
brand you selected for the item.
I’m pretty sure I have 2.1 but I can’t get :first or last to work in the
format you gave. It must be my syntax. Will post it later unless you can
provide a valid example?