So, I already have a list of cities added. I want to allow the user to
select from one of the available cities in the select. But, when I
submit the form, I get this error:
With accepts_nested_attributes you always just use the association
name - you don't need to mangle things. Also I'm slightly confused by
your models / form. Does a city really belong to a unique user ?
Ignore the model names and what they stand for :). They are only for
example purpose.
So, if I make my view like this:
<%= form_for :user,:url => users_path,:method => :post do |f| %>
Oh, for nested attributes to work you need an actual instance of user,
not the symbol :user
<%= f.fields_for :city do |b| %>
<%= b.collection_select :id,City.all,:id,:name %>
<% end %>
I'm not convinced that this will play well with nested attributes.
nested attributes mostly wants to either create a new object or edit
an existing one, whereas you want to choose from a list of objects and
then edit one of those (to set the user_id)
I guess it's expecting me to build the city instance in the controller?
correct. field_for on an association that has nested attributes turned
on renders its block for each associated object.
The way I got it to work is by doing this in my model:
def city_attributes=(attribs)
self.city = City.find(attribs[:id])
end
Is there no other way?
It might be easier to just have a city_id virtual attribute rather
than trying to bend accepts_nested_attributes_for (given that you are
currently overriding all of the stuff it gives you)
But, how would having it as a virtual attribute help? It would not be
saved to the database, so it's value would be lost.
because you'd define city_id= to be basically the same as the
city_attributes method you have above. It's not very different to what
you have but allows you to get rid of the accepts_nested_attributes
and the fields_for