简体   繁体   English

尝试使用活动存储上传文件时,无法在 rails 控制器中允许 :file 参数

[英]Unable to permit :file param in rails controller when trying to upload file using active storage

I'm running into a strange issue I've never seen before when setting up active storage where the file param isn't being permitted in my controller.我在设置活动存储时遇到了一个我以前从未见过的奇怪问题,而我的控制器中不允许使用文件参数。

Here's my model这是我的模型

class Upload < ApplicationRecord
  has_one_attached :file
  # ...removed for brevity
end

And the create method in my controller以及我的控制器中的 create 方法

class Api::V1::Agent::UploadsController < Api::V1::AgentController
  # ...removed for brevity

  # POST /uploads
  def create
    @upload = Upload.new(upload_params)

    if @upload.save
      render json: @upload, status: :created, location: @upload
    else
      render json: @upload.errors, status: :unprocessable_entity
    end
  end

  private

    # Only allow a trusted parameter "white list" through.
    def upload_params
      params.require(:upload).permit(:company_id, :employee_id, :title, :tags, :secure, :file)
    end
end

Here's what the params object looks like before calling require and permit .这是调用requirepermit之前 params 对象的样子。

<ActionController::Parameters {"company_id"=>1, "employee_id"=>"", "title"=>"Testing", "file"=>{"rawFile"=>{"path"=>"teams-icon.png"}, "src"=>"blob:http://localhost:3000/cee6f897-8a93-4e27-ab8e-724e6ec60f29", "title"=>"teams-icon.png"}, "format"=>:json, "controller"=>"api/v1/agent/uploads", "action"=>"create", "upload"=><ActionController::Parameters {"company_id"=>1, "employee_id"=>"", "title"=>"Testing s3 storage"} permitted: false>} permitted: false>

And then after然后之后

<ActionController::Parameters {"company_id"=>1, "employee_id"=>"", "title"=>"Testing s3 storage"} permitted: true>

If I throw a Pry breakpoint in #create I can call @upload.file and see the virtual file attribute, but for some reason the :file param isn't being permitted.如果我在 #create 中抛出一个 Pry 断点,我可以调用@upload.file并查看虚拟文件属性,但由于某种原因,不允许使用:file参数。

EDIT:编辑:

I've tried permitting the file param as a hash like so params.require(:upload).permit(file: {}) and got the same result.我试过允许文件参数作为散列,如params.require(:upload).permit(file: {})并得到相同的结果。

I also tried submitting the file as just a blob url string (eg {"company_id"=>1, "employee_id"=>"", "title"=>"Testing", "file"=>"blob:http://localhost:3000/1e72ade4-97fc-4535-88e9-45884bcd9633"} instead of the nested hash but :file is still dropped from the permitted params.我还尝试将文件作为 blob url 字符串提交(例如{"company_id"=>1, "employee_id"=>"", "title"=>"Testing", "file"=>"blob:http://localhost:3000/1e72ade4-97fc-4535-88e9-45884bcd9633"}而不是嵌套的哈希,但 :file 仍然从允许的参数中删除。

params[:file] 是一个散列,所以你必须使用一些不同的方式来允许它

params.require(:upload).permit(:company_id, :employee_id, :title, :tags, :secure, file: {})

The correct way to permit seems to be as a Array:允许的正确方法似乎是一个数组:

params.require(:upload).permit(:company_id, :employee_id, :title, :tags, :secure, file: [])

It works this way in the app I'm working on currently and is also how its documented in the rails doc .它在我目前正在开发的应用程序中以这种方式工作,也是它在 rails doc 中的记录方式

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何通过设计允许新参数进行主动存储? - How to permit a new param with devise for active storage? 通过 API 到 postman 使用 Rails 主动存储上传文件(.pdf、.jpg 等)? (不通过 Rails 视图) - upload a file(.pdf, .jpg etc) using rails active storage via API through postman? (not through rails views) Rails Strong参数:允许在控制器中使用参数 - Rails Strong Parameters: Permit a param in controller 如何在Rails 5中使用存储上载没有活动记录的文件? - How to use storage to upload a file without active record in Rails 5? Rails 6 Active storage sidekiq 文件在 S3 上上传背景图片 - Rails 6 Active storage sidekiq file upload background image on S3 如何将文件从 froala 编辑器上传到 Rails 活动存储 - how to upload a file from froala editor to rails active storage Active Storage 文件上传回调 - Callback for Active Storage file upload 使用 fixture_file_upload 上传文件时出现 Active Storage 问题 - Rspec - Active Storage issue when uploading a file using fixture_file_upload - Rspec 尝试将文件从Flex上传到Rails时使用例外(使用回形针) - Exception when trying to upload file from Flex to Rails (using paperclip) 如何允许文件字段上传Rails中的参数 - How to permit parameter from file field upload Rails
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM