繁体   English   中英

载波,Rails 4和多次上传

[英]Carrierwave, Rails 4, and Multiple Uploads

我一直把头撞在墙上,试图使Carrierwave,Rails 4和Multiple Uploads一起工作。 我可以像在这个项目和许多其他项目中一样,正常上传单个文件。

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

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

mount_uploader :document, DocumentUploader

作为强参数识别的数组。

我已尝试将白名单: whitelisted[:document] = params[:transcription]['document']

声明“文档”为数组:

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

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

这一切似乎更像是我为嵌套模型声明数组,但是我真的希望Rails 4的强大参数能够简单地看到由file_field创建的“文档”数组,:multiple => true

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

有人在参数强大的Rails 4中成功完成了多次上传吗? 如果是这样,请您分享一下?

谢谢...

干杯,

法案

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

为此,请按照下列步骤操作。

rails new multiple_image_upload_carrierwave

在宝石文件中

gem 'carrierwave'
bundle install
rails generate uploader Avatar 

创建后脚手架

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

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

在rails 3中,无需定义强参数,并且您可以在模型中定义attribute_accessible,并在发布模型中定义accept_nested_attribute,因为rails 4中已弃用了attribute。

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

暂无
暂无

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

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