Rails 4:如何使用carrierwave上传多张图片 [英] Rails 4: How to upload multiple images using carrierwave
问题描述
我可以使用carrierwave上传文件,如下面的代码所示.如何编辑代码以在一篇文章中上传最多 3 个文件的图片?
I can upload a file with using carrierwave as shown in the following code. How can I edit the code to make it possible to upload images up to 3 files in one article?
视图\文章\new.html.erb
views\articles\new.html.erb
..
视图\共享\ _article_form.html.erb
views\shared\ _article_form.html.erb
<%= f.submit class: "btn btn-large btn-primary" %><%结束%>..
.
.
<%= form_for(@article) do |f| %>
<div class="field">
<%= f.hidden_field :category_id %>
<%= f.fields_for :photos do |p| %>
<%= p.hidden_field :article_id %>
<% if p.object.image and p.object.image.file %>
<%= image_tag p.object.image.thumb.url %>
<p><%= p.object.image.file.filename %></p>
<% end %>
<%= p.file_field :image %>
<% end %>
<%= f.text_area :content, placeholder: "Enter contents..." %>
</div>
<%= f.submit class: "btn btn-large btn-primary" %>
<% end %>
.
.
\models\article.rb
\models\article.rb
class Article < ActiveRecord::Base
belongs_to :user
belongs_to :category
has_many :photos, dependent: :destroy
accepts_nested_attributes_for :photos
.
.
end
\models\photo.rb
\models\photo.rb
class Photo < ActiveRecord::Base
belongs_to :article
mount_uploader :image, ImageUploader
validates :image, presence: true
end
\controllers\articles_controller.rb
\controllers\articles_controller.rb
class ArticlesController < ApplicationController
.
.
def new
@article = Article.new
@category = Category.find(params[:category])
@article.category_id = @category.id
@article.photos.build
end
def create
@article = current_user.articles.build(article_params)
if @article.save
flash[:success] = "article created!"
redirect_to current_user #root_url
else
@feed_items = []
render 'new'
end
end
def edit
@article = Article.find(params[:id])
end
def update
@article = Article.find(params[:id])
if @article.update(article_params)
redirect_to current_user
else
render 'edit'
end
end
.
.
private
def article_params
params.require(:article).permit(:content, :category_id, photos_attributes: [:id, :article_id, :image])
end
.
.
end
推荐答案
将您的新操作更改为:
def new
@article = Article.new
@category = Category.find(params[:category])
@article.category_id = @category.id
@photo = @article.photos.build
end
并且在您的 file_field 中,您需要使用 multiple option
所以你的表单看起来像这样:
and in your file_field you need to use multiple option
so your form will look like this:
<%= form_for(@article,:html => { :multipart => true }) do |f| %>
// article fields
<%= f.fields_for @photo do |p| %>
<%= p.file_field :image, multiple: true %>
<% end %>
<% end %>
更新
改变你的新动作并制作 3 次照片
Change your new action and build a photo 3 times
def new
@article = Article.new
@category = Category.find(params[:category])
@article.category_id = @category.id
3.times { @article.photos.build }
end
和你的表格
<%= form_for(@article,:html => { :multipart => true }) do |f| %>
// article fields
<%= f.fields_for :photos do |p| %>
<%= p.file_field :image %>
<% end %>
<% end %>
此外,您还必须稍微修改模型以拒绝空白值
Also you'll have to modify your model a bit too in order to reject blank values
class Article < ActiveRecord::Base
belongs_to :user
belongs_to :category
has_many :photos, dependent: :destroy
accepts_nested_attributes_for :photos, reject_if: proc { |attributes| attributes[:image].blank? }
.
.
end
这篇关于Rails 4:如何使用carrierwave上传多张图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!