2015-05-08 37 views
1

在我的應用程序中,我創建了一個可以上載多個文檔的表單。現在我已經發布了帶有2個上傳文件的表單,但由於錯誤而無法保存在數據庫中。但是我得到了上面提到的所有參數。請幫我解決這個問題。未經允許的參數:文件導軌4

在控制器:

def create 
    @sr_document = SrDocument.new(sr_document_params) 
end 

def sr_document_params 
    params.require(:sr_document).permit(:file_type, :file, :service_request_id, :file_file_name, :file_content_type, :file_file_size) 
end 

在日誌中:

「sr_document」=> { 「文件」=> [

@臨時文件=#, @ original_filename = 「Reliance Web-Chat.pdf」, @ content_type =「application/pdf」,@ headers =「Content-Disposition: form-data; name = \」sr_document [file] [] \「;文件名= \ 「倚 網絡Chat.pdf \」 \ r \ nContent類型:應用程序/ PDF \ r \ n 「個>,

@tempfile =#\,@ original_filename =」 Flipkart.pdf」 ,@ content_type =「application/pdf」, @ headers =「Content-Disposition:form-data; name = \「sr_document [file] [] \」;文件名= \ 「Flipkart.pdf \」 \ r \ nContent類型:應用程序/ PDF \ r \ n「>

]}

+0

什麼是錯誤? – Sontya

+0

未經許可的參數:文件 –

+0

'file'是'sr_documents'表中的一個字段?你能向我們展示「形式」嗎? – Sontya

回答

3

你得到文件的數組,所以我認爲你是越來越問題: 儘量使你的文件屬性爲:

def sr_document_params 
    params.require(:sr_document).permit(:file_type, :file => [], :service_request_id, :file_file_name, :file_content_type, :file_file_size) 
end 
+0

語法錯誤:params.require(:sr_document).permit(file:[]) –

+0

嘗試這樣做「params.require(:sr_document).permit!」 –

+0

數組和散列參數必須位於「 :service_request_id,:file_file_name,...',雖然看起來像其他參數沒有使用。 –

1

這些文件存儲在一個數組,並像file_content_type文件的信息,...是每個文件的屬性,所以你不能像這樣。 請嘗試:

def create 
    sr_documents = [] 
    sr_document_params[:file].each do |file| 
    sr_documents << SrDocument.new({ file_content_type: file.content_type, 
             file_size: file.size, 
             file: file }) 
    end 
end 


def sr_document_params 
    params.require(:sr_document).permit(file: []) 
end 
+0

語法錯誤:params.require(:sr_document).permit(file:[]) –