【发布时间】:2018-05-11 04:21:22
【问题描述】:
我正在帮助一位朋友使用 ruby on rails 网站。我真的是 ruby on rails 的新手,开发该网站的开发人员很长。
该应用程序使用 activeadmin gem,目前文档部分中有一个列,据说该列以 PDF 格式导出文档,但无法正常工作。这是代码。来自 app/admin/documents.rb
ActiveAdmin.register Document do
index do
column :id
# This was buggy
column :user_name do |d|
d.user.nil? ? 'No User' : (link_to d.user.name, admin_user_path(d.user))
end
column :user_email do |d|
d.user.nil? ? 'No User' : d.user.email
end
column :price do |document|
document.price.nil? ? 'Gratis' : document.price
end
column :type_document
column :status
column :download do |document|
link_to 'Download PDF', document_path(document, format: :pdf)
end
default_actions
end
form do |f|
f.inputs "Edicion" do
f.semantic_errors
f.input :content
f.input :price
f.input :status, as: :select, collection: Document::STATUSES
f.actions
end
end
csv do
column :user_name
column :user_email
column :price
column('Tipo') { |document| document.type_document }
column :status
column(:content) { |document| strip_tags(document.content) }
end
end
我安装了 wicked_pdf gem,但我被困在这里。我不知道该怎么做。我找到了这个代码来生成 PDF
def generate_pdf(document)
pdf = WickedPdf.new.pdf_from_string(
document.content,
encoding: 'UTF-8',
page_size: 'A4',
orientation: 'Portrait',
template: 'documents/documento.pdf.rb',
margin: { top: 30, # default 10 (mm)
bottom: 30,
left: 20,
right: 20
},
layout: 'layouts/pdf.html'
)
send_data(
pdf,
filename: "document_#{document.type_document}_#{document.user.name}.pdf",
disposition: 'attachment'
)
end
我不确定该代码是否正确,以及如何将下载 PDF 链接链接到此功能?
任何帮助将不胜感激。
提前致谢
卡洛斯·索萨
【问题讨论】:
-
您可以添加一个使用活动管理操作项activeadmin.info/8-custom-actions.html#action-items 的按钮,然后从那里调用 generate_pdf 方法。
-
感谢您的回复。不知道如何从列中调用方法:下载以及在哪里定义方法。
标签: ruby-on-rails activeadmin wicked-pdf