2016-07-14 36 views
1

我在我的控制器,在軌道4,5工作以下罰款:我如何獲得在Rails 5中工作的多個文件上傳?

def create_multiple 
    params[:documents].map do |document| 
    if document[:upload] 
     doc = Document.new 
     doc.upload = document[:upload] 
     doc.category_id = @category.id 
     doc.save 
    end 
    end 
    redirect_to @category, notice: 'Documents saved' 
end 

現在,升級到Rails 5後,這是行不通的。我強烈懷疑這是因爲params is now an Object, rather than HashWithIndifferentAccess,但我不知道如何使多個文件上傳再次工作。

嘗試這樣:

params.to_unsafe_h[:documents].map do |document| 

但隨後失敗no implicit conversion of Symbol into Integerif document[:upload]部分。

關於如何在此方面前進的任何想法?

+0

看來'document'是一個數組?你不能用'調試器'或某些日誌記錄來驗證它嗎? –

回答

0

好吧,我不得不返工我的形式更「railsy」這在Rails的5工作,斯利姆(在new_multiple視圖):

= form_tag create_multiple_category_documents_path(@category), multipart: true do 
    .row.bottom30 
    - @documents.each_with_index do |document, ndx| 
     = fields_for 'documents[]', document, index: ndx do |f| 
     .col-xs-12.col-sm-6.col-md-4.bottom30 
      => f.label :title 
      = f.text_field :title, class: 'form-control' 
      = f.file_field :upload 

這裏現在什麼是控制器:

def new_multiple 
    @documents = 20.times.map { @category.documents.build } 
end 

def create_multiple 
    params[:documents].each do |num, document| 
    unless document[:upload].blank? 
     doc = Document.new 
     doc.upload = document[:upload] 
     doc.category_id = @category.id 
     doc.save 
    end 
    end 
    redirect_to @category, notice: 'Documents saved' 
end 

有可能是一個更好的方法來做到這一點,但現在這工作。

相關問題