wicked_pdf: ActionView::MissingTemplate after upgrade to rails 7
Issue description
We’ve been having issues with the render since our upgrade to rails 7.0 reporting that the template is not found even though the render code inside the controller didn’t change. I can confirm that running the same version of wicked_pdf on rails 6.1 still works fine in production.
# ...
render pdf: @title, layout: 'pdf', orientation: 'Landscape', template: 'controller/action.html.erb',
show_as_html: params.key?('debug'), margin: { top: 5, bottom: 5, left: 5, right: 5 },
footer: { right: 'Page [page] / [topage]', font_size: 8 }
# ...
Expected or desired behavior
Renders the pdf like usual
System specifications
wicked_pdf gem version: master
on github
wkhtmltopdf version: 0.12.6
platform/distribution and version: docker ruby 3.0.3 alpine image
About this issue
- Original URL
- State: open
- Created 2 years ago
- Reactions: 5
- Comments: 29 (6 by maintainers)
Hello and thanks for this issue! I was having the same problem and was able to solve it using:
template: 'controller/action', formats: [:html]
Note that is
formats
instead offormat
This is what worked
@guemidiborhane I’m not sure why this is yet, and am looking into it, but I was able to get it to work by changing
template: 'controller/action.html.erb'
, totemplate: 'controller/action'
. This should be a valid work-around for now.Please try this and let me know how it goes!
I found myself with a similar error after upgrading to Rails 7. This was my initial working code:
Which returned this error after the upgrade:
ActionView::MissingTemplate (Missing template layouts/pdf.html.erb with {:locale=>[:it], :formats=>[:pdf], :variants=>[], :handlers=>[:raw, :erb, :html, :builder, :ruby]}
And this is how I had to change:Additionally I had to rename
layouts/pdf.html.erb
tolayouts/pdf.pdf.erb
, otherwise it was ignored. Noformats
required in my case, as per other users.@hybridspyda Let’s schedule a quick screen-share for me to help you solve your issue. Shoot me an email and we’ll coordinate a time.
@unixmonkey
Thanks for the pointing me in the right direction. I was able to get it working by:
For anyone else who is struggling with this:
WORKED IN RAILS 6.1 BUT NOT RAILS 7:
WORKS IN RAILS 7:
For us, the solution was to rename the view files. many were like
action_pdf.haml
we made themaction.pdf.haml
@srbong Here’s what I’d do to troubleshoot your issue:
print_media_type
line, and maybe thedisposition
one (if it makes it easier to refresh the page).html
off the template string, does it render or give you a better error messageformats: :html
and try againformats :pdf
and try againmake_and_send_pdf
’srender(render_opts)
(which is Rails’s render)I hope this helps some. Let me know if you are still having trouble, maybe send me an email and we can screenshare pair on it.
@gabriel-andreoli Ok, so this error message tells me that your render call is causing Rails to look for:
app/views/lotes/gerar_etiqueta.pdf.erb
(the:formats=>[:pdf]
part), which is overriding your request forformat: :html
.So either you can either:
.pdf.erb
instead of.html.erb
, and remove theformat: :html
option (which is the incorrect option name anyway) or:format: :html
toformats: [:html]
.No guarantees, but I think this will fix things for you.
Had this same issue with Rails 7 in production. I ran
bundle update wkhtmltopdf-binary
so it’s now 0.12.6.6 (was 0.12.6.5) and it’s now working.Another gotcha: We were using
formats: "html"
, which did not work anymore with Rails 7.0. However, changing it to a symbolformats: :html
fixed the issue 🙄this does not work for footer and header under rails 7 unfortunately.
Does not throw an error like before but just does not displays the footer. Does anyone have an idea why?
EDIT:
The trick was to change the setting call for the footer to
Hello!!!, I have this issue
How can I solve it?
Thanks @yshmarov we haven’t merged our
rails7
branch yet to production, so we’ll keep an eye about this…@spaci76 I recommend you get on the latest version of this gem.
I was facing the same error since I upgraded to Rails 7.
My view file was named
export_pdf.pdf.haml
and my controller code looked liketemplate: 'proposals/export_pdf.pdf.haml'
.I had to rename my file to
export_pdf.html.haml
and changed controller code totemplate: 'proposals/export_pdf'
No need to setformats
option.@hybridspyda Maybe it can help you 😃
I can confirm that using
formats
fixes this issue.This issue can probably be closed since this is really caused by an underlying Rails change. My guess is that PDF generation is the only place I’ll see this in my app because it’s the only place where I want to render html when the request type is not “.html”.
@aliciapaz sorry for the typo, the issue is that it wasn’t required previously