英文:
Rails ActiveStorage - without model
问题
I have a Feature where user uploads an XML file and the system should read the file and import data. So the file is not attached to any Model Record. So I have a form which allows file input.
<%= form_with(
multipart: true,
scope: :ctf,
url: some_path,
id: dom_id(Import.new, :form),
data: { turbo: false }
) do |form| %>
<%= render Content::SectionComponent.new do |section| %>
<span class="text-sm"><%= 'Upload XML' %></span>
<%= form.label :cf_file, 'File' %>
<%= form.file_field :cf_file, accept: 'application/xml', required: true %>
<% end %>
<% end %>
In the controller, I get the file as a param and manually upload it using ActiveStorage::Blob.
def cf_upload
importer = Import.new(cf_upload_params)
if importer.valid?
file_storage = ActiveStorage::Blob.create_and_upload!(io: cf_upload_params[:cf_file], filename: filename)
Importers::Cf::ImportJob.perform_later(file_storage)
else
flash.now[:alert] = 'Something went wrong'
end
render :index
end
Is there any way I can use ActiveStorage direct_upload for this instead of doing upload using ActiveStorage::Blob.create_and_upload?
英文:
I have a Feature where user uploads an XML file and the system should read the file and import data. So the file is not attached to any Model Record. So I have a form which allows file input
<%= form_with(
multipart: true,
scope: :ctf,
url:some_path,
id: dom_id(Import.new, :form),
data: { turbo: false }
) do |form| %>
<%= render Content::SectionComponent.new do |section| %>
<span class="text-sm"><%= 'Upload XML' %></span>
<%= form.label :cf_file, 'File' %>
<%= form.file_field :cf_file, accept: 'application/xml', required: true %>
<% end %>
<% end %>
In the controller, I get the file as param and manually upload it using ActiveStorage::Blob.
def cf_upload
importer = Import.new(cf_upload_params)
if importer.valid?
file_storage = ActiveStorage::Blob.create_and_upload!(io: cf_upload_params[:cf_file], filename: filename)
Importers::Cf::ImportJob.perform_later(file_storage)
else
flash.now[:alert] = 'Something went wrong'
end
render :index
end
Is there any way I can use ActiveStorage direct_upload for this instead of doing upload using ActiveStorage::Blob.create_and_upload?
答案1
得分: 1
根据文档,ActiveStorage::Blob
是一个数据库记录,并且它是根据ActiveRecord模型设计的。
一个可能可行的选择是使用直接上传,这实际上允许你设置一些JavaScript,使文件直接上传到你的存储服务(例如S3),而你的表单提交的是令牌或上传文件的标识符。
但这个解决方案也建立在ActiveStorage::Blob
已创建的假设上。所以你可能需要绕过它的方式。
但如果你在问 - 你能创建Blob并将其上传到服务,而不将其附加到任何其他模型吗 - 绝对可以。未附加的Blob是存在的,这就是为什么文档正在解释如何清除它们以便进行清理的原因。
英文:
According to the docs ActiveStorage::Blob
is a database record, and it has been designed with ActiveRecord models in mind.
One possibly viable option for you would be to use Direct Uploads which essentially allows you to set up some JS so the file is uploaded directly to your storage service (e.g. S3) and what your form submits is a token, or the identifier of the uploaded file.
But this solution is also built on the assumption the ActiveStorage::Blob
is created. So probably you'd need to hack your way around it.
But if you're asking - can you create the blob and upload it to the service without attaching it to any other model - absolutely. Unatached blobs are a thing, which is why the docs are explaining how to purge them if you want to clean up house.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论