3

我无法使用服务对象中的 Active Storage 将 JSON Tempfile 附加到模型。这很容易复制:

模型:

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.)

我在模型上data_file定义为。不知道问题可能在这里。stringServiceRequest

4

1 回答 1

12

您不能将 Tempfile 对象单独传递给#attach. 您必须传递一个包含 open :io、 a:filename和 a的 Hash :content_type

@service_request.data_file.attach(io: temp_file, filename: "data.json", content_type: "application/json")

有关它接受的参数的更多信息,请参阅文档。#attached

于 2018-02-11T23:28:06.377 回答