[英]Rails4: Template is missing error with PDFKit
我想生成 PDF 文件,所以我嘗試使用 PDFKit 但失敗了。
單擊鏈接時顯示以下錯誤。
ActionView::MissingTemplate (Missing template /show with {:locale=>[:en], :formats=>[:pdf], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :jbuilder]}. Searched in:
* "/usr/local/rvm/gems/ruby-2.3.0/gems/web-console-2.0.0.beta3/lib/action_dispatch/templates"
* "/home/ubuntu/workspace/app/views"
* "/usr/local/rvm/gems/ruby-2.3.0/gems/web-console-2.0.0.beta3/app/views"
時間表\\show.html.erb
<% provide(:title, @schedule.title) %>
<%= render @schedules %>
時間表\\_schedule.html.erb
...
<%= link_to "PDF", schedule_path(schedule.id, format: "pdf"), class: "btn btn-sm btn-default" %>
...
schedules_controller.rb
...
respond_to do |format|
format.html # show.html.erb
format.pdf do
html = render_to_string template: "show"
pdf = PDFKit.new(html, encoding: "UTF-8")
send_data pdf.to_pdf,
filename: "#{@scheudles.id}.pdf",
type: "application/pdf",
disposition: "inline"
end
end
...
雖然我創建show.pdf.erb
和_schedule.pdf.erb
其內容是一樣的html.erb
,結果是一樣的。
您獲得Missing template /show
的原因是因為使用template: "show"
的控制器不夠具體,rails 無法知道在您的視圖文件夾中的哪個位置可以找到show
文件。
你可以在你的錯誤中看到這方面的證據,它說它在../app/views
搜索show
。 您需要使用模板父文件夾和模板名稱。
改變這個
format.pdf do
html = render_to_string template: "show"
...
end
對此
format.pdf do
html = render_to_string template: "schedules/show.html.erb"
...
end
只需更改:action => "show.html.erb"
html = render_to_string(:layout => false , :action => "show.pdf.erb")
希望這對你有幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.