使用 ActiveStorage 错误附加临时文件
Attaching Tempfile with ActiveStorage error
我无法将 JSON 临时文件附加到使用 服务对象 中的活动存储的模型。这很容易复制:
型号:
class ServiceRequest < ApplicationRecord
has_one_attached :data_file
进程:
temp_file = Tempfile.new([SecureRandom.uuid, '.json'])
@service_request.data_file.attach(temp_file)
错误:
ActiveRecord::RecordNotSaved (Failed to save the new associated data_file_attachment.)
我在 ServiceRequest
模型上将 data_file
定义为 string
。不确定这里可能是什么问题。
您不能将 Tempfile 对象单独传递给 #attach
。您必须传递包含开放 :io
、:filename
和 :content_type
:
的哈希
@service_request.data_file.attach(io: temp_file, filename: "data.json", content_type: "application/json")
有关 #attached
接受的参数的详细信息,请参阅 the docs。
我无法将 JSON 临时文件附加到使用 服务对象 中的活动存储的模型。这很容易复制:
型号:
class ServiceRequest < ApplicationRecord
has_one_attached :data_file
进程:
temp_file = Tempfile.new([SecureRandom.uuid, '.json'])
@service_request.data_file.attach(temp_file)
错误:
ActiveRecord::RecordNotSaved (Failed to save the new associated data_file_attachment.)
我在 ServiceRequest
模型上将 data_file
定义为 string
。不确定这里可能是什么问题。
您不能将 Tempfile 对象单独传递给 #attach
。您必须传递包含开放 :io
、:filename
和 :content_type
:
@service_request.data_file.attach(io: temp_file, filename: "data.json", content_type: "application/json")
有关 #attached
接受的参数的详细信息,请参阅 the docs。