简体   繁体   English

载波,Rails 4和多次上传

[英]Carrierwave, Rails 4, and Multiple Uploads

I have been banging my head against the wall trying to get Carrierwave, Rails 4, and Multiple Uploads all working together. 我一直把头撞在墙上,试图使Carrierwave,Rails 4和Multiple Uploads一起工作。 I can get a single file upload working just fine as in this and many other projects. 我可以像在这个项目和许多其他项目中一样,正常上传单个文件。

This is not a nested situation - just simply uploading to a single model called Transcription and wanting to create a record for each document uploaded. 这不是一个嵌套的情况-只是简单地上传到一个名为Transcription的模型,并希望为每个上传的文档创建一条记录。

I cannot seem to find the correct way to declare the "document" field used for the carrierwave mount 我似乎找不到正确的方法来声明用于载波安装的“文档”字段

mount_uploader :document, DocumentUploader

as an array for the strong parameters to recognize. 作为强参数识别的数组。

I have tried whitelisting: whitelisted[:document] = params[:transcription]['document'] , 我已尝试将白名单: whitelisted[:document] = params[:transcription]['document']

declaring the "document" as an array: 声明“文档”为数组:

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

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

This all seems more like I am declaring the array for a nested model, but I really want Rails 4's strong parameters to simply see the "document" array created by the file_field, :multiple => true 这一切似乎更像是我为嵌套模型声明数组,但是我真的希望Rails 4的强大参数能够简单地看到由file_field创建的“文档”数组,:multiple => true

ie. 即。 from the log: form-data; name=\\"transcription[document][] 从日志: form-data; name=\\"transcription[document][] form-data; name=\\"transcription[document][]

Has anybody successfully accomplished multiple uploads in Rails 4 with strong parameters? 有人在参数强大的Rails 4中成功完成了多次上传吗? If so would you please share? 如果是这样,请您分享一下?

Thanks... 谢谢...

Cheers, 干杯,

Bill 法案

This is solution to upload multiple images using carrierwave in rails 4 from scratch 这是从头开始在rails 4中使用载波上传多个图像的解决方案

To do just follow these steps. 为此,请按照下列步骤操作。

rails new multiple_image_upload_carrierwave

In gem file 在宝石文件中

gem 'carrierwave'
bundle install
rails generate uploader Avatar 

Create post scaffold 创建后脚手架

rails g scaffold post title:string

Create post_attachment scaffold 创建post_attachment支架

rails g scaffold post_attachment post_id:integer avatar:string

rake db:migrate

In post.rb 在post.rb中

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

In post_attachment.rb 在post_attachment.rb中

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

In post_controller.rb 在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

In views/posts/_form.html.erb 在views / posts / _form.html.erb中

<%= form_for(@post, :html => { :multipart => true }) do |f| %>
   <div class="field">
     <%= f.label :title %><br>
     <%= f.text_field :title %>
   </div>

   <%= f.fields_for :post_attachments do |p| %>
     <div class="field">
       <%= p.label :avatar %><br>
       <%= p.file_field :avatar, :multiple => true, name: "post_attachments[avatar][]" %>
     </div>
   <% end %>

   <% if params[:controller] == "post" && params[:action] == "edit" %> 
     <% @post.post_attachments.each do |p| %>
       <%= image_tag p.avatar, :size => "150x150" %>
     <% end %>
   <% end %>

   <div class="actions">
     <%= f.submit %>
   </div>
<% end %>

In views/posts/show.html.erb 在views / posts / show.html.erb中

<p id="notice"><%= notice %></p>

<p>
  <strong>Title:</strong>
  <%= @post.title %>
</p>

<% @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 %>

In rails 3 no need to define strong parameters and as you can define attribute_accessible in both the model and accept_nested_attribute to post model because attribute accessible is deprecated in rails 4. 在rails 3中,无需定义强参数,并且您可以在模型中定义attribute_accessible,并在发布模型中定义accept_nested_attribute,因为rails 4中已弃用了attribute。

CarrierWave doesn't support multiple uploads. CarrierWave不支持多个上传。 It's designed to associate a single file with a single field. 它旨在将单个文件与单个字段相关联。

If you want multiple uploads, you need either multiple fields (each with a CarrierWave uploader), or multiple objects each with a single CarrierWave uploader field. 如果要进行多次上传,则需要多个字段(每个都有一个CarrierWave上传器),或者需要多个对象,每个对象都有一个CarrierWave上传器字段。

The multiple attribute is also unsupported, so if you use it, it's entirely up to you to get the parameters assigned properly. 另外,也不支持multiple属性,因此,如果您使用它,则完全取决于您是否正确分配了参数。

I would create a model called Documents with a field that's mounted 我将创建一个名为Documents的模型,该模型具有已安装的字段

class Documents < ActiveRecord::Base
  belongs_to :transcription

  mount_uploader :doc, DocumentUploader
end

class Transcriptions < ActiveRecord::Base
  has_many :documents
end

And I would still have user the below line in my controller: 而且我的控制器中仍将有以下行的用户:

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

The best method for this that I have come across is using the native approach of CarrierWave . 我遇到的最好的方法是使用CarrierWave的本机方法。 If you already have single file upload done, with the native approach it takes less than 5 minutes to get multiple file upload. 如果您已经完成了单个文件上传,则使用本机方法只需不到5分钟即可获得多个文件上传。 https://github.com/carrierwaveuploader/carrierwave#multiple-file-uploads https://github.com/carrierwaveuploader/carrierwave#multiple-file-uploads

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM