1

我无法让我的 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
4

2 回答 2

2

你有has_many :images 所以,应该是:images_attributes,不是:image_attributes

InventoryItem.create!(:name => 'what', :images_attributes => [ {:image => File.open("/home/davidc/Desktop/letterbx.jpg", "r") }])

当你有has_many关系时使用哈希数组是正确的

于 2010-07-02T21:27:46.890 回答
0

:image_attributes应该是一个哈希。

InventoryItem.create!(
   :name => 'what',
   :image_attributes => { ... }
)
于 2010-07-02T17:32:27.903 回答