Active Storage Rails 6 API 无法插入到 active_storage_attachments table
Active Storage Rails 6 API unable to insert to active_storage_attachments table
我正在为使用 reactjs 的活动存储创建 API 端点。我的上传控制器如下:下面的代码执行:
在 active_storage_blob 中插入 blob 对象 - 确定
上传文件到指定存储路径 - ok
不插入active_storage_attachments(多态关系)我已经加了
has_one_attached:照片
class Api::V1::Activestorage::DirectUploadsController <
ActiveStorage::DirectUploadsController
skip_before_action :verify_authenticity_token
def create
blob = ActiveStorage::Blob.create_before_direct_upload!(**blob_args)
render json: direct_upload_json(blob)
end
private
def blob_args
params.require(:blob).permit(:filename, :byte_size, :checksum, :content_type, metadata: {}).to_h.symbolize_keys
end
def direct_upload_json(blob)
blob.as_json(root: false, methods: :signed_id).merge(direct_upload: {
url: blob.service_url_for_direct_upload,
headers: blob.service_headers_for_direct_upload
})
end
end
除了这个active_storage_attachmenttable都没有插入。希望有人能帮忙。谢谢大家
我添加了一个新的附件方法,这是我的重构代码。希望这会有所帮助。
class Api::V1::UploadController < ActiveStorage::DirectUploadsController
skip_before_action :verify_authenticity_token
def create
blob = ActiveStorage::Blob.create_before_direct_upload!(blob_args)
render json: direct_upload_json(blob)
end
def attached_create
ActiveStorage::Attachment.create(params_attached) #from react axios pass params
end
private
def params_attached
params.require(:attachment).permit(:name, :record_type, :record_id, :blob_id)
end
def blob_args
params.require(:blob).permit(:filename, :byte_size, :checksum, :content_type, metadata: {}).to_h.symbolize_keys
end
def direct_upload_json(blob)
blob.as_json(root: false, methods: :signed_id).merge(service_url: url_for(blob)).merge(direct_upload: {
url: blob.service_url_for_direct_upload,
headers: blob.service_headers_for_direct_upload
})
end
我正在为使用 reactjs 的活动存储创建 API 端点。我的上传控制器如下:下面的代码执行:
在 active_storage_blob 中插入 blob 对象 - 确定
上传文件到指定存储路径 - ok
不插入active_storage_attachments(多态关系)我已经加了 has_one_attached:照片
class Api::V1::Activestorage::DirectUploadsController < ActiveStorage::DirectUploadsController skip_before_action :verify_authenticity_token def create blob = ActiveStorage::Blob.create_before_direct_upload!(**blob_args) render json: direct_upload_json(blob) end private def blob_args params.require(:blob).permit(:filename, :byte_size, :checksum, :content_type, metadata: {}).to_h.symbolize_keys end def direct_upload_json(blob) blob.as_json(root: false, methods: :signed_id).merge(direct_upload: { url: blob.service_url_for_direct_upload, headers: blob.service_headers_for_direct_upload }) end end
除了这个active_storage_attachmenttable都没有插入。希望有人能帮忙。谢谢大家
我添加了一个新的附件方法,这是我的重构代码。希望这会有所帮助。
class Api::V1::UploadController < ActiveStorage::DirectUploadsController
skip_before_action :verify_authenticity_token
def create
blob = ActiveStorage::Blob.create_before_direct_upload!(blob_args)
render json: direct_upload_json(blob)
end
def attached_create
ActiveStorage::Attachment.create(params_attached) #from react axios pass params
end
private
def params_attached
params.require(:attachment).permit(:name, :record_type, :record_id, :blob_id)
end
def blob_args
params.require(:blob).permit(:filename, :byte_size, :checksum, :content_type, metadata: {}).to_h.symbolize_keys
end
def direct_upload_json(blob)
blob.as_json(root: false, methods: :signed_id).merge(service_url: url_for(blob)).merge(direct_upload: {
url: blob.service_url_for_direct_upload,
headers: blob.service_headers_for_direct_upload
})
end