Rails 3: How does “accepts_nested_attributes_for” work?

北慕城南 提交于 2019-11-28 17:13:38

I think that you're trying to figure out creating a new associated item vs. associating with an existing item.

For creating a new item, you seem to have it working. When you passed the id in shop_attributes, it did not work, because it's looking up an association that doesn't exist yet.

If you're trying to associate with an existing item, you should be using the following:

params[:product][:shop_id] = "20"

This will assign the current product's shop to the shop with id 'shop_id'. (Product should have a 'shop_id' column.)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!