David C
David C

Reputation: 3739

Unknown Attributes on nested form in Rails

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

Upvotes: 1

Views: 4288

Answers (2)

Tatjana N.
Tatjana N.

Reputation: 6225

You have has_many :images So, it should be :images_attributes, not :image_attributes

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

And it is correct to use array of hashes when you have has_many relationship

Upvotes: 2

Ju Nogueira
Ju Nogueira

Reputation: 8461

:image_attributes is supposed to be a hash.

InventoryItem.create!(
   :name => 'what',
   :image_attributes => { ... }
)

Upvotes: 0

Related Questions