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:
- I currently have a user.rb (using devise) and post.rb. User has many post and Post belongs to user.
- I need to create a new model, picture.rb.
- In picture.rb, have belongs_to :post
- Create a controller for picture (picture_controller). But what goes in the controller?
- In picture.rb, have required Paperclip code:
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.