2

I have a deep nested form with offering having a gallery which has photos.

offering.rb

has_one :gallery, foreign_key: "offering_id",:inverse_of => :offering, dependent: :destroy
accepts_nested_attributes_for :gallery, allow_destroy: true

gallery.rb

belongs_to :offering, foreign_key: "offering_id",:inverse_of => :gallery
has_many :photos, foreign_key: :gallery_id, dependent: :destroy, inverse_of: :gallery
accepts_nested_attributes_for :photos, allow_destroy: true, reject_if: proc { |attributes| attributes['image_file_name'].blank? }

photo.rb

belongs_to :gallery has_attached_file :image, :styles=>{:photo => "600x400#"}

offerings_controller.rb

def new
    @offering = Offering.new()
    [email protected]_gallery
    5.times{gallery.photos.build}
    @vendor=Vendor.find(params[:vendor_id])
end

def create
    @vendor=Vendor.find(params[:vendor_id])
    @[email protected](offering_params)
    if @offering.save
        redirect_to vendor_path(params[:vendor_id])
    else
        [email protected]_gallery
        5.times{gallery.photos.build}
        render 'new'
    end
end

private
    def offering_params
        params.require(:offering).permit(gallery_attributes: [:id,:offering_id,{photos_attributes: [:id,:gallery_id,:image]}])
    end

offerings/new.html.erb

<%= form_for [@vendor,@offering],:html=>{:multipart => true} do |f| %>
    <%= f.fields_for :gallery do |g| %>
        Pictures for Gallery
        <%= g.fields_for :photos do |p| %>
            <%= p.file_field(:image) %>
        <% end %>
   <% end %>
<% end %>


Parameters: {"utf8"=>"✓", "authenticity_token"=>"AApeyzQbi6TxYYKFG/alcea+rK9s/MP2o8X4vJS741o=", "offering"=>{"gallery_attributes"=>{"photos_attributes"=>{"0"=>{"image"=>#<ActionDispatch::Http::UploadedFile:0x007fd2ea60aad0 @tempfile=#<Tempfile:/tmp/RackMultipart20150515-5596-nywang>, @original_filename="11261457_694846000643900_1047203131_n.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"offering[gallery_attributes][photos_attributes][0][image]\"; filename=\"11261457_694846000643900_1047203131_n.jpg\"\r\nContent-Type: image/jpeg\r\n">}, "2"=>{"image"=>#<ActionDispatch::Http::UploadedFile:0x007fd2ea60a8c8 @tempfile=#<Tempfile:/tmp/RackMultipart20150515-5596-1d988rp>, @original_filename="11271093_695669323894901_362202707_n.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"offering[gallery_attributes][photos_attributes][2][image]\"; filename=\"11271093_695669323894901_362202707_n.jpg\"\r\nContent-Type: image/jpeg\r\n">}}}}, "commit"=>"Add your listing", "vendor_id"=>"10"}}

The problem is that although gallery is saving in the database, the photos are not inserting. It is not even attempting to insert via any query.

1
  • Hey I have the same logic in my rails 5 app. Can you tell me which was your real solution, please? Or any clue you could provide for me? Thanks in advance! Commented Oct 19, 2016 at 2:21

2 Answers 2

1

The reject_if: proc { |attributes| attributes['image_file_name'].blank? } is expecting an attribute in the actual hash of params, as per your whitelist you only have :id, :gallery_id, or :image available. That reject would have to be one of those otherwise it'll never be found.

Sign up to request clarification or add additional context in comments.

Comments

1

The error was in

accepts_nested_attributes_for :photos, allow_destroy: true, reject_if: proc { |attributes| attributes['image_file_name'].blank? }

it had to be

accepts_nested_attributes_for :photos, allow_destroy: true, reject_if: proc { |attributes| attributes['image'].blank? }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.