Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 使用wicked和Rails API创建PDF的问题_Ruby On Rails_Pdf_Rails Api_Wicked Pdf - Fatal编程技术网

Ruby on rails 使用wicked和Rails API创建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

我正在尝试使用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:{: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
参考: