0

I'm creating an app that needs to be able to allow users to upload multiple images to a post. I'm using Paperclip to upload images, it's currently only uploading one. I'm having a very hard time trying to understand how to implement it onto my existing structure to allow for multiple images.

The process I was thinking goes like this, I'm very new to programming in general so let me know if I'm mistaken. I will bold steps that I'm more unsure about then others:

  1. I currently have a user.rb (using devise) and post.rb. User has many post and Post belongs to user.
  2. I need to create a new model, picture.rb.
  3. In picture.rb, have belongs_to :post
  4. Create a controller for picture (picture_controller). But what goes in the controller?
  5. In picture.rb, have required Paperclip code: enter image description here

The following are the part that I'm most confused about

What goes in post_controller and picture_controller? Here's my current post_controller:

class PostsController < ApplicationController
    before_action :find_posts, only: [:show, :edit, :update, :destroy, :upvote, :downvote]
    before_action :authenticate_user!, except: [:index, :show, :home]

    def home
    end

    def index
        if params[:category].blank?
            @posts = Post.all.order("created_at DESC")
        else
            @category_id = Category.find_by(name: params[:category]).id  
            @posts = Post.where(category_id: @category_id).order("created_at DESC")
        end
    end

    def show
        @inquiries = Inquiry.where(post_id: @post).order("created_at DESC")
        @random_post = Post.where.not(id: @post).order("RANDOM()").first
    end

    def new
        @post = current_user.posts.build
    end

    def create
        @post = current_user.posts.build(post_params)
        if @post.save 
            redirect_to @post 
        else 
            render 'new'
        end
    end

    def edit
    end

    def update
        if @post.update(post_params)
            redirect_to @post 
        else
            render 'edit'
        end
    end

    def destroy
        @post.destroy
        redirect_to root_path
    end

    def upvote
        @post.upvote_by current_user
        redirect_to @post
    end

    def downvote
        @post.downvote_by current_user
        redirect_to @post
    end

    private 
    def find_posts 
        @post = Post.find(params[:id])
    end

    def post_params
        params.require(:post).permit(:title, :price, :description, :location, :category_name, :contact_number, :image)
    end
end

How do I change my post/form to accept multiple images from picture? Currently:

.edit-container
= simple_form_for @post, html: { multipart: true } do |f| 
    .edit-form
        = f.input :title
        = f.input :location, disabled: true
        = f.input :price
        = f.input :description
        = f.input :contact_number, placeholder: "(999) 999-9999"
        = f.label "Category"
        = f.text_field :category_name, data: {autocomplete_source: Category.order(:name).map(&:name)}, placeholder: "Choose a category"
        = f.file_field :image, multiple: true, name: "post[image]"
        = f.button :submit
%script
    $('#post_location').val("#{request.location.city}, #{request.location.state}")

And lastly, I know I need to do something like

- @picture.each do |image|

But it didn't work when I tried it. Here's what I have now:

.clearfix
    .post_image_description
        = image_tag @post.image.url if @post.image?
        .description= simple_format(@post.description)
        .description 
            Contact:
            = @post.contact_number

and this is the index

#posts
.row
    - @posts.each do |post|
        .col-xs-12.col-sm-6.col-md-4.col-lg-3
            .post
                .post_content
                    .title
                        %h2
                            = link_to truncate((post.title), length: 25), post
                    %p
                        $
                        = post.price
                    .post_image
                        = link_to image_tag(post.image.url), post

I hope this is enough information for a genuine and detailed answer. I've worked on this for nearly a week trying to figure this out, so I really hope someone can help me figure this out.

Loi Huynh
  • 408
  • 1
  • 4
  • 12

3 Answers3

0

You may find the carrierwave gem to be better for this project, and easier to setup. You won't need a seperate picture model.

Follow the instructions here:

https://github.com/carrierwaveuploader/carrierwave

In your case you'd need something like this:

class Post < ActiveRecord::Base
  mount_uploaders :pictures, PictureUploader
end
Lewis Buckley
  • 1,583
  • 15
  • 22
0

After about a week or so of trying to get it to work, I decided that carrierwave was a better choice and followed this answer to implement it to my current app. It is very detailed (although you will need to adjust it to your current app).

Community
  • 1
  • 1
Loi Huynh
  • 408
  • 1
  • 4
  • 12
-1

You could benefit from jquery file upload. It is a very easy way to deal with multiple file uploads through jquery. Read through the documentation and let me know if anything is unclear.

user1943992
  • 222
  • 3
  • 11