1
votes

In my application i am creating a form which uploads multiple document. Now i have posted the form with 2 upload documents but not able to save in the database due to the error. But I am getting all the parameters as mentioned above. Please help me out to solve this issue.

In controller:

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

In log:

"sr_document"=>{"file"=>[

@tempfile=#, @original_filename="Reliance Web-Chat.pdf", @content_type="application/pdf", @headers="Content-Disposition: form-data; name=\"sr_document[file][]\"; filename=\"Reliance Web-Chat.pdf\"\r\nContent-Type: application/pdf\r\n">,

@tempfile=#\, @original_filename="Flipkart.pdf", @content_type="application/pdf", @headers="Content-Disposition: form-data; name=\"sr_document[file][]\"; filename=\"Flipkart.pdf\"\r\nContent-Type: application/pdf\r\n">

]}

2
Unpermitted parameter: file - Uday kumar das
file is a field in sr_documents table? can you show us the form? - Sonalkumar sute

2 Answers

3
votes

You are getting array of file, so that I think you are getting issue: Try to permit your file attribute as:

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
1
votes

These files are stored in an array and the file's information like file_content_type,.. are attributes of each file, so you can't get it like this. Please try:

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