【问题标题】:Unpermitted parameter: file rails 4不允许的参数:file rails 4
【发布时间】:2023-03-23 17:39:01
【问题描述】:

在我的应用程序中,我正在创建一个上传多个文档的表单。现在我已经发布了带有 2 个上传文档的表单,但由于错误而无法保存在数据库中。但是我得到了上面提到的所有参数。请帮我解决这个问题。

在控制器中:

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"=>{"file"=>[

@tempfile=#, @original_filename="Reliance Web-Chat.pdf", @content_type="application/pdf", @headers="Content-Disposition: 表格数据;名称=\"sr_document[文件][]\";文件名=\“信赖 Web-Chat.pdf\"\r\nContent-Type: application/pdf\r\n">,

@tempfile=#\, @original_filename="Flipkart.pdf", @content_type="application/pdf", @headers="内容配置:表单数据; 名称=\"sr_document[文件][]\"; filename=\"Flipkart.pdf\"\r\nContent-Type: application/pdf\r\n">

]}

【问题讨论】:

  • 不允许的参数:文件
  • filesr_documents 表中的字段吗?你能告诉我们form吗?

标签: ruby-on-rails ruby-on-rails-4


【解决方案1】:

您正在获取文件数组,因此我认为您遇到了问题: 尝试允许您的文件属性为:

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

【讨论】:

  • 语法错误:params.require(:sr_document).permit(file: [])
  • 试试这个“params.require(:sr_document).permit!”
  • 数组和哈希参数必须在最后,在:service_request_id, :file_file_name,...之后,虽然看起来其他参数没有被使用。
【解决方案2】:

这些文件存储在一个数组中,文件的信息像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

【讨论】:

  • 语法错误:params.require(:sr_document).permit(file: [])
猜你喜欢
  • 2013-08-01
  • 2013-07-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-11
相关资源
最近更新 更多