Carrierwave,Rails 4和多个上传

我一直在撞墙试图让Carrierwave,Rails 4和Multiple Uploads一起工作。 我可以让单个文件上传工作得很好,就像在这个和许多其他项目中一样。

这不是嵌套的情况 – 只需上传到名为Transcription的单个模型,并希望为上传的每个文档创建记录。

我似乎找不到正确的方法来声明用于载波装载的“文档”字段

mount_uploader :document, DocumentUploader 

作为要识别的强参数的数组。

我试过白名单: whitelisted[:document] = params[:transcription]['document']

将“document”声明为数组:

 params.require(:transcription).permit(..... ,:document => []) params.require(:transcription).permit(..... , { document: [] }) 

这似乎更像是我为嵌套模型声明数组,但我真的希望Rails 4的强参数只是看到file_field创建的“document”数组,:multiple => true

即。 来自日志: form-data; name=\"transcription[document][] form-data; name=\"transcription[document][]

有没有人在Rails 4中使用强参数成功完成多次上传? 如果是这样,请分享一下?

谢谢…

干杯,

法案

这是从头开始在rails 4中使用carrierwave上传多个图像的解决方案

要执行这些步骤。

 rails new multiple_image_upload_carrierwave 

在gem文件中

 gem 'carrierwave' bundle install rails generate uploader Avatar 

创建post脚手架

 rails g scaffold post title:string 

创建post_attachment脚手架

 rails g scaffold post_attachment post_id:integer avatar:string rake db:migrate 

在post.rb

 class Post < ActiveRecord::Base has_many :post_attachments accepts_nested_attributes_for :post_attachments end 

在post_attachment.rb中

 class PostAttachment < ActiveRecord::Base mount_uploader :avatar, AvatarUploader belongs_to :post end 

在post_controller.rb中

 def show @post_attachments = @post.post_attachments.all end def new @post = Post.new @post_attachment = @post.post_attachments.build end def create @post = Post.new(post_params) respond_to do |format| if @post.save params[:post_attachments]['avatar'].each do |a| @post_attachment = @post.post_attachments.create!(:avatar => a, :post_id => @post.id) end format.html { redirect_to @post, notice: 'Post was successfully created.' } else format.html { render action: 'new' } end end end def update respond_to do |format| if @post.update(post_params) params[:post_attachments]['avatar'].each do |a| @post_attachment = @post.post_attachments.create!(:avatar => a, :post_id => @post.id) end end end def destroy @post.destroy respond_to do |format| format.html { redirect_to @post } format.json { head :no_content } end end private def post_params params.require(:post).permit(:title, post_attachments_attributes: [:id, :post_id, :avatar]) end 

在views / posts / _form.html.erb中

 <%= form_for(@post, :html => { :multipart => true }) do |f| %> 
<%= f.label :title %>
<%= f.text_field :title %>
<%= f.fields_for :post_attachments do |p| %>
<%= p.label :avatar %>
<%= p.file_field :avatar, :multiple => true, name: "post_attachments[avatar][]" %>
<% end %> <% if params[:controller] == "post" && params[:action] == "edit" %> <% @post.post_attachments.each do |p| %> <%= image_tag p.avatar, :size => "150x150" %> <% end %> <% end %>
<%= f.submit %>
<% end %>

在views / posts / show.html.erb中

 

<%= notice %>

Title: <%= @post.title %>

<% @post_attachments.each do |p| %> <%= image_tag p.avatar_url, :size => "150x150" %> <%= link_to "Destroy", p, method: :delete %> <% end %> <%= link_to 'Edit', edit_post_path(@post) %> | <%= link_to 'Back', posts_path %>

在rails 3中,无需定义强参数,因为您可以在模型和accept_nested_attribute中定义attribute_accessible以发布模型,因为在rails 4中不推荐使用可访问属性。

CarrierWave不支持多次上传。 它旨在将单个文件与单个字段相关联。

如果您想要多次上传,则需要多个字段(每个字段都有一个CarrierWave上传器),或者多个对象,每个字段都有一个CarrierWave上传器字段。

multiple属性也不受支持,因此如果您使用它,完全取决于您正确分配参数。

我将创建一个名为Documents的模型,其中包含已安装的字段

 class Documents < ActiveRecord::Base belongs_to :transcription mount_uploader :doc, DocumentUploader end class Transcriptions < ActiveRecord::Base has_many :documents end 

我仍然可以在我的控制器中使用以下行:

 params.require(:transcription).permit(..... , { document: [] }) 

我遇到的最好的方法是使用CarrierWave的原生方法。 如果您已经完成了单个文件上传,使用本机方法可以在不到5分钟的时间内完成多个文件上传。 https://github.com/carrierwaveuploader/carrierwave#multiple-file-uploads