comfy/comfy-blog

Nested attribute not saving in rails 4.

Closed this issue · 3 comments

I am using mexicon sofa(1.11.0) in rails 4 and trying to save the nested attributes but it is not saving.
The problem is:

gallery.rb

class Gallery < ActiveRecord::Base
has_attached_file :top_image, :styles => {:thumb => "200x200>" }
has_many :gallery_images, :dependent => :destroy
accepts_nested_attributes_for :gallery_images, :reject_if => lambda { |a| a[:content].blank? }, :allow_destroy => true
end

galery_image.rb

class GalleryImage < ActiveRecord::Base
 has_attached_file :image, :styles => {:thumb => "200x200>" }
 belongs_to :gallery
end

_form.html.haml

= form.text_field :title
= form.text_field :description
= form.file_field :top_image
= form.fields_for :gallery_images do |builder|
= render "gallery_image_fields", :form => builder
 %p= link_to_add_fields "Add Image", form, :gallery_images
 .form-actions
   = form.submit :class => 'btn btn-primary'

_gallery_image_fields.html.haml

= form.file_field :image
= form.hidden_field :_destroy
= link_to_remove_fields "Remove Image", form 

gallery_controller.rb

def gallery_params
  params.fetch(:gallery, {}).permit(:title, :description, :top_image, gallery_images_attributes: [:id, :image, :_destroy])
 end

Parametrs in the server logs

 Parameters: {"utf8"=>"✓", "authenticity_token"=>"d7Zb8wVKVU6omi9Aicvr4WIgz+TcN7Cl40w9wneBhas=", "gallery"=>{"title"=>"gfhgf", "description"=>"hfghfghgf", "gallery_images_attributes"=>{"1390804105999"=>{"image"=>#<ActionDispatch::Http::UploadedFile:0xcaa7d94 @tempfile=#<Tempfile:/tmp/RackMultipart20140127-3901-tz2cb9>, @original_filename="abc.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"gallery[gallery_images_attributes][1390804105999][image]\"; filename=\"abc.jpg\"\r\nContent-Type: image/jpeg\r\n">, "_destroy"=>"false"}}}, "commit"=>"Create Gallery"}

Everything is running fine even it is sending the image attribute but it is not saving. Can anyone tell me where is the problem?

GBH commented

This doesn't really have anything to do with Blog, but I don't see anything particularly wrong with your code. I'd try relaxing params to params.fetch(:gallery, {}).permit! and see if that helps.

Thanks for your reply,, but it is still not working. In the server logs i am getting

{"utf8"=>"✓", "authenticity_token"=>"N86EVxHFq6/8KWnucXhrJD4akFU+vssNXm4ILMK7dXY=", "gallery"=>{"title"=>"yuiyu", "description"=>"iyuiyuiyu", "top_image"=>#<ActionDispatch::Http::UploadedFile:0xc64ed9c @tempfile=#<Tempfile:/tmp/RackMultipart20140128-3127-hfzayw>, @original_filename="Screenshot from 2013-12-13 14:35:04.png", @content_type="image/png", @headers="Content-Disposition: form-data; name=\"gallery[top_image]\"; filename=\"Screenshot from 2013-12-13 14:35:04.png\"\r\nContent-Type: image/png\r\n">, "gallery_images_attributes"=>{"1390884783725"=>{"image"=>#<ActionDispatch::Http::UploadedFile:0xc64ecc0 @tempfile=#<Tempfile:/tmp/RackMultipart20140128-3127-s3mfcw>, @original_filename="Screenshot from 2013-12-13 14:34:38.png", @content_type="image/png", @headers="Content-Disposition: form-data; name=\"gallery[gallery_images_attributes][1390884783725][image]\"; filename=\"Screenshot from 2013-12-13 14:34:38.png\"\r\nContent-Type: image/png\r\n">, "_destroy"=>"false"}}}, "commit"=>"Create Gallery", "action"=>"create", "controller"=>"admin/galleries"}
GBH commented

That's odd. Watch the log. Does it even attempt to do inserts?