Ruby on rails 使用wicked和Rails API创建PDF的问题
我正在尝试使用wicked和Rails API创建一个可下载的PDF。目前我只能下载PDF,但内容是空的,文件名是response.PDF.PDF 这是我在向特定分数发出GET请求时用来生成PDF的方法Ruby on rails 使用wicked和Rails API创建PDF的问题,ruby-on-rails,pdf,rails-api,wicked-pdf,Ruby On Rails,Pdf,Rails Api,Wicked Pdf,我正在尝试使用wicked和Rails API创建一个可下载的PDF。目前我只能下载PDF,但内容是空的,文件名是response.PDF.PDF 这是我在向特定分数发出GET请求时用来生成PDF的方法 def download_pdf(score) html = render_to_string(:action => :show, :layout => "pdf.html.erb", :template => "scores/show.pdf.erb", locals
def download_pdf(score)
html = render_to_string(:action => :show, :layout => "pdf.html.erb", :template => "scores/show.pdf.erb", locals:{:score => score})
pdf = WickedPdf.new.pdf_from_string(html)
send_data(pdf,
:filename => 'test.pdf',
:disposition => 'attachment')
end
我今天遇到了这个问题。虽然已经很晚了,但对于寻求解决方案的人来说还是很有用的 您必须从ActionController::Base继承,而不是从ActionController::API继承。或者,您可以使用以下代码覆盖render_to_字符串
def render_to_string(*args)
controller = ActionController::Base.new
controller.locale = I18n.locale
controller.render_to_string(*args)
end
参考: