从活动管理员中下载 PDF

Download a PDF from within active admin

作为我应用程序中的管理员用户,我可以创建会员并附加 PDF 文件(使用回形针)

在我看来,我希望能够单击 link 下载该特定会员的 PDF 文档

因此 Active Admin 允许您创建自定义操作(它还会为您生成路线)

 ActiveAdmin.register Membership do
   collection_action :download_pdf, method: :get do

   end
end

在我看来我有

column 'File' do |f|
  link_to('Download Membership Form', download_pdf_admin_memberships_path)
end

试着把我的碎片拼起来

collection_action :download_pdf, method: :get do
  membership = Membership.find(params[:id])
  send_data(filename: "#{membership.file_file_name}.pdf",
            type: 'application/pdf'
           )
end

column 'File' do |f|
  link_to('Download Membership Form', download_pdf_admin_memberships_path(f))
end

目前这会生成 url 的 http://localhost:3000/admin/memberships/download_pdf.60

点击时出现错误

Couldn't find Membership with 'id'=

我不确定如何让 URL 正确构建(通过 id)并实际下载 PDF?

可以通过方法获取会员id吗?

要下载文件,您最好使用 send_file:

send_file membership.file.path

同时修改您的 link_to 以正确传递对象的 ID,并指定 format: :pdf:

link_to(
  'Download Membership Form',
  download_pdf_admin_memberships_path(id: object.id, format: :pdf)
)