使用预先存在的模型和控制器实现 Paperclip 多图像上传
Implementing Paperclip multiple image upload with preexisting models and controllers
我正在创建一个需要能够允许用户将多张图片上传到 post 的应用程序。我正在使用 Paperclip 上传图片,目前只上传一张。我很难理解如何将它实现到我现有的结构上以允许多个图像。
我想的过程是这样的,我对一般的编程很陌生所以如果我弄错了请告诉我。我会比其他人更不确定的大胆步骤:
- 我目前有一个 user.rb(使用设计)和 post.rb。用户有多个 post 并且 Post 属于用户。
- 我需要创建一个新模型,picture.rb。
- 在picture.rb中,有belongs_to:post
- 为图片创建一个控制器(picture_controller)。 但是控制器里有什么?
- 在picture.rb中,需要回形针代码:
以下是我最困惑的部分
post_controller 和 picture_controller 中的内容是什么?这是我目前的 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
如何更改 post/form 以接受图片中的多张图片?目前:
.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}")
最后,我知道我需要做类似
的事情
- @picture.each do |image|
但是我试了一下,还是不行。这是我现在拥有的:
.clearfix
.post_image_description
= image_tag @post.image.url if @post.image?
.description= simple_format(@post.description)
.description
Contact:
= @post.contact_number
这是索引
#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
我希望这些信息足以提供真实而详细的答案。我已经为此工作了将近一个星期,试图解决这个问题,所以我真的希望有人能帮我解决这个问题。
您可以从 jquery file upload 中受益。通过jquery处理多个文件上传是一种非常简单的方法。通读文档,如果有任何不清楚的地方,请告诉我。
您可能会发现 carrierwave
gem 更适合此项目,并且更易于设置。您不需要单独的图片模型。
按照此处的说明进行操作:
https://github.com/carrierwaveuploader/carrierwave
在你的情况下,你需要这样的东西:
class Post < ActiveRecord::Base
mount_uploaders :pictures, PictureUploader
end
在尝试让它工作大约一周后,我认为 carrierwave 是更好的选择,并按照 answer 将其应用到我当前的应用程序中。它非常详细(尽管您需要根据您当前的应用程序对其进行调整)。
我正在创建一个需要能够允许用户将多张图片上传到 post 的应用程序。我正在使用 Paperclip 上传图片,目前只上传一张。我很难理解如何将它实现到我现有的结构上以允许多个图像。
我想的过程是这样的,我对一般的编程很陌生所以如果我弄错了请告诉我。我会比其他人更不确定的大胆步骤:
- 我目前有一个 user.rb(使用设计)和 post.rb。用户有多个 post 并且 Post 属于用户。
- 我需要创建一个新模型,picture.rb。
- 在picture.rb中,有belongs_to:post
- 为图片创建一个控制器(picture_controller)。 但是控制器里有什么?
- 在picture.rb中,需要回形针代码:
以下是我最困惑的部分
post_controller 和 picture_controller 中的内容是什么?这是我目前的 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
如何更改 post/form 以接受图片中的多张图片?目前:
.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}")
最后,我知道我需要做类似
的事情- @picture.each do |image|
但是我试了一下,还是不行。这是我现在拥有的:
.clearfix
.post_image_description
= image_tag @post.image.url if @post.image?
.description= simple_format(@post.description)
.description
Contact:
= @post.contact_number
这是索引
#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
我希望这些信息足以提供真实而详细的答案。我已经为此工作了将近一个星期,试图解决这个问题,所以我真的希望有人能帮我解决这个问题。
您可以从 jquery file upload 中受益。通过jquery处理多个文件上传是一种非常简单的方法。通读文档,如果有任何不清楚的地方,请告诉我。
您可能会发现 carrierwave
gem 更适合此项目,并且更易于设置。您不需要单独的图片模型。
按照此处的说明进行操作:
https://github.com/carrierwaveuploader/carrierwave
在你的情况下,你需要这样的东西:
class Post < ActiveRecord::Base
mount_uploaders :pictures, PictureUploader
end
在尝试让它工作大约一周后,我认为 carrierwave 是更好的选择,并按照 answer 将其应用到我当前的应用程序中。它非常详细(尽管您需要根据您当前的应用程序对其进行调整)。