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 に答える