Ruby on Rails: Help in multiple image upload using paperclip with different process

Lorelyn

I have 3 models: Post, Comment, and Image

(I am using paperclip gem BTW)

What I am trying to achieve is that users can comment on post. Also, if they opt to comment images in the post, they could do so. The relationship looks like this:

For Post

class Post < ApplicationRecord
  belongs_to :user
  has_many :comments, :dependent => :destroy
end

For Comment

class Comment < ApplicationRecord
  belongs_to :user
  belongs_to :post
  has_many :images, :dependent => :destroy
end

and for Image

class Image < ApplicationRecord
  belongs_to :user
  belongs_to :comment
end

The process is that for example, they would like to comment on a post, and would like to attach an image, they could do so by attaching an image to the comment. They could also attach multiple image in one comment.

However, I can't seem to know how to do that. Upon researching, I stumbled this post which upload multiple images in a gallery.

However, it is only two layer model (Gallery and Picture). He makes use of this code in his view and controller:

View

<%= file_field_tag "images[]", type: :file, multiple: true %>

controller

if params[:images]
  params[:images].each { |image|
    @market.pictures.create(image: image)
  }
end

I get on how to do that. But it is different from what I'm trying to achieve. If you can help me, that would be great! Thanks in advance!

Aman

there may be different approach to upload but i would like to prefer this approach here with nested attributes and cocoon gem here you can get an example

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Rails 4: Multiple image upload using paperclip

How to Upload Multiple Image in Rails 4 Using Paperclip

Rails Multiple image upload (paperclip, carrerwave)

Paperclip image upload in rails

How can i image upload on another Ruby on Rails application using paperclip?

Active admin multiple image upload with paperclip Rails 5

Can't upload zip files using ruby on rails and paperclip gem

Access image from different view in a view with paperclip gem ruby on rails

Rails 4 - Paperclip - Upload an image using DATA URI

Seeding a model with a paperclip image in ruby on rails

Rails upload a picture using paperclip and devise

Ruby on Rails: Deleting multiple attachments in paperclip on updates

How to upload image into Gmaps4rails's marker using Paperclip?

Show image from another model using id upload by paperclip Rails 3

get only first image using paperclip rails

Carrierwave for image upload with ruby on rails

Ruby on Rails paperclip questions

Ruby On Rails: Get md5 hash of paperclip image

How to preview uploaded image instantly with paperclip in ruby on rails

Paperclip image not uploads with faker gem data in ruby on rails?

Rails direct upload to Amazon S3 using Activeadmin + Paperclip

Paperclip image resizing in rails

Upload Image From seed file using paperclip gem

Rails 4 multiple image or file upload using carrierwave

Converting all input fields to button using Paperclip Gem in Ruby on Rails

getting "undefined method `url' for "":String" Using paperclip in Ruby on Rails

Understanding Server Set-up for Using ImageMagick and PaperClip with Ruby on Rails

Rails Paperclip upload button not working

Rails file upload (paperclip) on edit