Rails 中嵌套表单的未知属性
我无法让我的 InventoryItem 接受嵌套属性,这很奇怪。
在我的脚本/控制台中,我执行了以下操作:
>> InventoryItem.create!(:name => 'what', :image_attributes => [ {:image => File.open("/home/davidc/Desktop/letterbx.jpg", "r") }])
ActiveRecord::UnknownAttributeError: unknown attribute: image_attributes
我不确定为什么在我的模型中收到未知属性错误,我已经执行了accept_nested_attributes。
我正在使用 Rails v2.3.5。
库存商品型号
class InventoryItem < ActiveRecord::Base
uuid_it
belongs_to :user
has_many :orders
has_many :images, :validate => true
accepts_nested_attributes_for :images
end
图片
class Image < ActiveRecord::Base
belongs_to :inventory_item
has_attached_file :image, :style => { :medium => "300x300>", :thumb => "100x100>" }
end
I'm having trouble getting my InventoryItem to accept nested attributes which is strange.
In my script/console, I did the following:
>> InventoryItem.create!(:name => 'what', :image_attributes => [ {:image => File.open("/home/davidc/Desktop/letterbx.jpg", "r") }])
ActiveRecord::UnknownAttributeError: unknown attribute: image_attributes
I am not sure why I'm getting the unknown attribute error when in my model, I already did accept_nested_attributes.
I'm using Rails v2.3.5.
Inventory Item Model
class InventoryItem < ActiveRecord::Base
uuid_it
belongs_to :user
has_many :orders
has_many :images, :validate => true
accepts_nested_attributes_for :images
end
Image
class Image < ActiveRecord::Base
belongs_to :inventory_item
has_attached_file :image, :style => { :medium => "300x300>", :thumb => "100x100>" }
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您有
has_many :images
所以,它应该是
:images_attributes
,而不是:image_attributes
当你有
has_many
关系时,使用哈希数组是正确的You have
has_many :images
So, it should be
:images_attributes
, not:image_attributes
And it is correct to use array of hashes when you have
has_many
relationship:image_attributes
应该是一个哈希值。:image_attributes
is supposed to be a hash.