I'd like to generate PDF file, so I am trying to use PDFKit but failing.
The following error was displayed when I click the link.
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"
schedules\show.html.erb
<% provide(:title, @schedule.title) %>
<%= render @schedules %>
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
...
Although I create show.pdf.erb
and _schedule.pdf.erb
which contents is the same as html.erb
, the result is the same.
The reason you are getting Missing template /show
is because from the controller using template: "show"
isn't specific enough for rails to know where in your views folder to find the show
file.
You can see the evidence of this in your error where it says it searched for show
inside ../app/views
. You need to use the template parent folder along with the template name.
change this
format.pdf do
html = render_to_string template: "show"
...
end
to this
format.pdf do
html = render_to_string template: "schedules/show.html.erb"
...
end