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

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

Lorelyn Published in 2018-01-06 22:49:57Z

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

For Comment

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

and for Image

class Image < ApplicationRecord
  belongs_to :user
  belongs_to :comment

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:


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


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

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!

Gabbar Reply to 2018-01-07 04:33:08Z

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

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.299387 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO