Ruby on rails 获得';未定义的方法“视图路径=';对于#<;ActionView::LookupContext:0x00007fdb191d6cc0>;你是说?查看路径';在Rails中使用PDFKit时出错

Ruby on rails 获得';未定义的方法“视图路径=';对于#<;ActionView::LookupContext:0x00007fdb191d6cc0>;你是说?查看路径';在Rails中使用PDFKit时出错,ruby-on-rails,ruby,wkhtmltopdf,pdfkit,actionview,Ruby On Rails,Ruby,Wkhtmltopdf,Pdfkit,Actionview,我正在使用PDFKit(以及Gem render_anywhere和wkhtmltopdf二进制文件)创建一个按钮,用于从Rails中的HTML创建PDF。我学习了两个教程,都以“发票”为例。我的应用程序使用“审计”。我以为我已经跟踪了所有的tee,但是在ActionView::LookupContext对象上得到了一个错误:未定义的方法'view\u paths='# 我正在使用的教程: 下载\u控制器: class DownloadsController < ApplicationCo

我正在使用PDFKit(以及Gem render_anywhere和wkhtmltopdf二进制文件)创建一个按钮,用于从Rails中的HTML创建PDF。我学习了两个教程,都以“发票”为例。我的应用程序使用“审计”。我以为我已经跟踪了所有的tee,但是在ActionView::LookupContext对象上得到了一个错误:
未定义的方法'view\u paths='#

我正在使用的教程:

下载\u控制器

class DownloadsController < ApplicationController


   def show
    respond_to do |format|
      format.pdf { send_audit_pdf }
    end
  end

  private

  def audit_pdf
    audit = params[:incoming]
    AuditPdf.new(audit)
  end

  def send_audit_pdf
    send_file audit_pdf.to_pdf,
      filename: audit_pdf.filename,
      type: "application/pdf",
      disposition: "inline"
  end

end
我的
views/layout/audit\u pdf.html.erb
如下所示:

<!DOCTYPE html>
<html>
<head>
  <title>Audit PDF</title>
  <style>
    <%= Rails.application.assets.find_asset('audit.pdf').to_s %>
  </style>
</head>
<body>
  <%= yield %>
</body>
</html>

审计PDF

因为您只使用rails api,所以您面临这个问题

你需要改变

def as_html
  render template: "audits/pdf", layout: "audit_pdf", locals: { audit: audit }
end
致:

def as_html
  render template: "audits/pdf", layout: "audit_pdf", locals: { audit: audit }
end
def pdf_html
  ActionController::Base.new.render_to_string(template: 'audits/pdf.html.erb', layout: 'audit_pdf.erb')
end