Trying to save some images in my product form. I'm excpecting paremeters where I would have images_attributes being a part of "product". When I make parameters like that in the console and create a Product, the images actually save.
class Product < ActiveRecord::Base
extend FriendlyId
friendly_id :name, use: :slugged
attr_accessible :description, :name, :category_ids, :brand_ids, :image_ids, :images_attributes
has_many :images, :as => :imageable
accepts_nested_attributes_for :images
end
class Image < ActiveRecord::Base
belongs_to :imageable, :polymorphic => true
attr_accessible :name, :file
mount_uploader :file, ImageUploader
end
= simple_form_for(@product, :html => {:multipart => true}) do |f|
= f.error_notification
.form-inputs
= f.input :name
= f.input :description
= f.association :categories, as: :check_boxes
= f.association :brands, as: :check_boxes
= f.association :images
= simple_fields_for :images do |i|
= i.input :file, as: :file
= i.input :name
.form-actions
= f.button :submit
# GET /products/new
# GET /products/new.json
def new
@product = Product.new
@product.images.build
respond_to do |format|
format.html # new.html.erb
format.json { render json: @product }
end
end
{
"utf8"=>"✓",
"authenticity_token"=>"vvXZFh9sJivA3i4Y0rx9i/oqLwKByrExgYisfdj/N78=",
"product"=> {
"name"=>"sxsad",
"description"=>"saasd",
"category_ids"=>[""],
"brand_ids"=>[""],
"image_ids"=>[""]
},
# should be images_attributes and come straight after image_ids?
"images"=>{
"name"=>"sdfsdfsdf"
},
"commit"=>"Create Product"
}
Once i've got it working for one image I will look into something like Cocoon for multiple images. Any thoughts on where this could be going wrong would be greatly appreciated :).
You should simply write:
= f.simple_fields_for :images do |i|