Nested paper clip with multiple images

I have a one-to-many relationship between the Banana model and the image model.

In addition, each banana and image belong to the user (through a separate association, because the image and its bananas may have different users).

I need a nested form for creating bananas as well as for images. A kicker is that I don’t know how many images need to be created (note the multiple attribute). The aforementioned bit of the form below will create the corresponding number of images, but will not complete the corresponding link to the user. Is there a way to do this using fields_for (so the associations are complete) as I tried?

BANANA MODEL

class Banana < ActiveRecord::Base belongs_to :user validates_presence_of :user has_many :images, dependent: :destroy accepts_nested_attributes_for :images validates_associated :images end 

IMAGE MODEL

 class Image < ActiveRecord::Base belongs_to :user validates_presence_of :user belongs_to :banana validates_presence_of :banana has_attached_file :img end 

FORM

 <%= form_for @banana, :validate => true, :html => { :multipart => true } do |f| %> <!-- <input type="file" name="banana[images_attributes][][img]" multiple />--> <%= f.fields_for 'images_attributes[]', @banana.images do |builder| %> <%= builder.file_field :img, multiple: true %> <% end %> <% end %> 

CONTROLLER

 class BananasController < ApplicationController def create @banana = current_user.bananas.build(banana_params) render :new unless @banana.save end def new @banana = Banana.new end private def banana_params params.required(:banana).permit(images_attributes: [:img]) end end 
+4
source share
1 answer

Paperclip multiple downloads in role

Mark this answer!

You can use paperclip to upload photos and nested_form to upload multiple times. The above question will help you relate all this. If you still cannot do this, write to me. I just solve this problem.

+2
source

Source: https://habr.com/ru/post/1499821/


All Articles