如何在用户下载 ActiveStorage blob 附件时更新数据库?

How to update the database when users download an ActiveStorage blob attachment?

目前用户可以使用以下 link:

在我的应用程序中下载 ActiveStorage blob
link_to 'download', rails_blob_path(pj.document.file, disposition: 'attachment')

但是,我想更新数据库中的一个属性,以便关联模型在首次下载文件时进行注册。此字段称为 downloaded_at 字段。

我做了以下尝试:

  1. 在更新模型时更改了 link_to > button_to。
  2. 添加了适当的路线
  3. 在数据库中添加了以下代码:

    def download
        @proofreading_job = ProofreadingJob.find(params[:id])
        @proofreading_job.update(downloaded_at: Time.current) if current_user == @proofreading_job.proofreader.user
        response.headers["Content-Type"] = @proofreading_job.document.file.content_type
        response.headers["Content-Disposition"] = "attachment; #{@proofreading_job.document.file.filename.parameters}"
    
        @proofreading_job.document.file.download do |chunk|
          response.stream.write(chunk)
        end
        ensure
        response.stream.close
    end
    

但是,除了重定向到我想要的@proofreading_job 页面之外,这没有做任何事情。

有没有人以前做过这个,如果有我该如何完成这个任务。

我想你也可以尝试使用你的动作控制器作为代理,概念是这样的:

  1. 在您的操作中下载文件
  2. 检查是否下载成功及其他验证
  3. 执行清理操作(在您的情况下是在您的 #3 中添加的代码)
  4. 使用send_data/send_file渲染方法将文件发回给用户

例如在你的控制器中:

def download
  file = open(params[:uri])
  validate!
  cleanup!
  send_file file.path
end

那么在你看来:

link_to 'download', your_controller_path

以上只是概念,很抱歉只提前提供了伪代码

最后我只是用了一些javascript来捕捉按钮的点击,如下所示:

    td = link_to rails_blob_path(pj.document.file, disposition: 'attachment'), 
         id: pj.document.id, 
   download: pj.document.file_name, 
      class: "btn btn-outline-secondary btn-sm btn-download" do
        =pj.document.file_name 
        i.fa.fa-download.ml-3 aria-hidden="true"

咖啡脚本:

  $('.btn-download').on 'click', (e) ->
    id = $(this).attr('id')
    $.ajax {url: Routes.document_path(id), type: 'PUT'}

routes.rb

resources :documents, only: [:show, :update]

documents_controller.rb:

  def update
    document = Document.find(params[:id])
    authorize([:proofreaders, document]) 
    document.update(downloaded_at: Time.current) if document.downloaded_at.nil?
    head :ok
  end

这似乎很有效。它更新数据库,用户将文件下载到他们的计算机上。