Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

turbo_frame_request_id: Could not render layout: undefined method `headers' for nil:NilClass #1084

Open
miguelpeniche opened this issue Nov 8, 2023 · 1 comment

Comments

@miguelpeniche
Copy link

Console error:

irb(main):002> enrollment.save_diploma
  User Load (3.3ms)  SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2  [["id", 2], ["LIMIT", 1]]
/Users/miguel/.asdf/installs/ruby/3.2.2/lib/ruby/gems/3.2.0/gems/turbo-rails-1.5.0/app/controllers/turbo/frames/frame_request.rb:36:in `turbo_frame_request_id': Could not render layout: undefined method `headers' for nil:NilClass (NoMethodError)

      request.headers["Turbo-Frame"]
             ^^^^^^^^
/Users/miguel/.asdf/installs/ruby/3.2.2/lib/ruby/gems/3.2.0/gems/turbo-rails-1.5.0/app/controllers/turbo/frames/frame_request.rb:36:in `turbo_frame_request_id': undefined method `headers' for nil:NilClass (NoMethodError)

      request.headers["Turbo-Frame"]

Method:

def save_pdf template, locals, orientation: "portrait", page_size: "Letter"
    name = template.split("/").last
    pdf_file = WickedPdf.new.pdf_from_string(
      ActionController::Base.new.render_to_string(
        template: "pdfs/#{template}",
        locals: locals,
        encoding: "UTF-8"
      ),
      pdf: to_s,
      page_size: page_size,
      orientation: orientation,
      margin: {top: 0, bottom: 0, left: 0, right: 0}
    )
    tempfile = Tempfile.new([name, ".pdf"], Rails.root.join("tmp"))
    tempfile.binmode
    tempfile.write pdf_file
    tempfile.close
    if pdf_file.present?
      update "#{name}": tempfile
      tempfile.unlink
    end
  end

MacOS Sonoma 14.2
wicked_pdf (2.6.3)
wkhtmltopdf 0.12.6 (with patched qt)

Showing the file is ok, but trying to save the file with Carrierwave return this error

@patchfx
Copy link

patchfx commented Jan 23, 2024

You maybe able to work around this by using the ApplicationController renderer. This worked in my example.

Change your render_to_string to this:

ApplicationController.renderer.render(
        template: "pdfs/#{template}",
        locals: locals,
        encoding: "UTF-8"
      )

Hope this helps!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants