2010-07-02 73 views
1

我無法讓我的InventoryItem接受奇怪的嵌套屬性。Rails中嵌套窗體的未知屬性

在我的腳本/控制檯,我做了以下內容:

>> 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 

回答

0

:image_attributes應該是散列。

InventoryItem.create!(
    :name => 'what', 
    :image_attributes => { ... } 
) 
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關係

使用哈希數組