Reputation: 225
I'm building a project in rails, it's a virtual store for a company. The user should be able to register new products and upload images, so I generated 2 models: "Product" and "Image", Product has_many Images, and a Image belongs_to product, I already ran paperclip to update the Image. "accepts_nested_attributes_for :images
" is in Product model, so I can save the images from the same product form. The thing is that the images are not being saved. The user should be able to upload 3 images (this is how I allow it "3.times {@product.images.build}
"). This is my Product Controller:
def new
@product = Product.new
3.times {@product.images.build}
end
def create
@product = Product.new(params[:product])
if @product.save
redirect_to :root
else
render :action => 'new'
end
end
I also tried defining the product_params method like this:
private
def product_params
params.require(:product).permit(:title, :info, :description, :price, :images_attributes => [])
end
This is the Request information thrown in the console:
Request
Parameters:
{"utf8"=>"✓",
"authenticity_token"=>"qRyYjG9pSaFxgCtMddDN3fpbsTeIAagLEz+psd+Z+oHa2AVjXpXYcbxta/Egj2TGrmF3FFNCllkY54dig3aN8g==",
"product"=>{"title"=>"Title",
"info"=>"Infor",
"description"=>"Description",
"price"=>"9",
"images_attributes"=>{"0"=>{"photo"=>#<ActionDispatch::Http::UploadedFile:0xad3d830 @tempfile=#<Tempfile:/tmp/RackMultipart20150820-9643-h6wdta.png>,
@original_filename="sticker,
375x360.u1.png",
@content_type="image/png",
@headers="Content-Disposition: form-data; name=\"product[images_attributes][0][photo]\"; filename=\"sticker,
375x360.u1.png\"\r\nContent-Type: image/png\r\n">},
"1"=>{"photo"=>#<ActionDispatch::Http::UploadedFile:0xad3d704 @tempfile=#<Tempfile:/tmp/RackMultipart20150820-9643-19t1r3v.png>,
@original_filename="sticker,
375x360.u1.png",
@content_type="image/png",
@headers="Content-Disposition: form-data; name=\"product[images_attributes][1][photo]\"; filename=\"sticker,
375x360.u1.png\"\r\nContent-Type: image/png\r\n">},
"2"=>{"photo"=>#<ActionDispatch::Http::UploadedFile:0xad3d650 @tempfile=#<Tempfile:/tmp/RackMultipart20150820-9643-fj3w5g.png>,
@original_filename="sticker,
375x360.u1.png",
@content_type="image/png",
@headers="Content-Disposition: form-data; name=\"product[images_attributes][2][photo]\"; filename=\"sticker,
375x360.u1.png\"\r\nContent-Type: image/png\r\n">}}},
"commit"=>"Submit"}
If I pass the method product_params
as a parameter in @product = Product.new(product_params)
, it save the title, description and price but no the images.
Upvotes: 2
Views: 282
Reputation: 34338
You kept :images_attributes => []
but it should be :images_attributes => [:image]
So, change this:
params.require(:product).permit(:title, :info, :description, :price, :images_attributes => [])
to:
params.require(:product).permit(:title, :info, :description, :price, :images_attributes => [:image])
in your product_params
method.
It should work after that!
For the image validation use this:
validates_attachment_presence :image
If this validation does not work, then try to use a custom validation like this:
validate :image_present
def image_present
if image_file_name.blank?
errors.add(:image, :image not present")
end
end
Upvotes: 1