簡體   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