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

Issue with render_to_string method of ActionController. "Could not render layout: undefined method headers" #529

Closed
limjinsun opened this issue Nov 28, 2023 · 3 comments · Fixed by #532

Comments

@limjinsun
Copy link

Ruby version 3.2.2
Rails version 7.0.8

GemFile

source 'https://rubygems.org'

ruby '3.2.2'

gem 'rails', '7.0.8'
gem 'puma', '~> 4.3.9'
gem 'sass-rails', '~>6.0.0'
gem 'jquery-rails'
gem 'sprockets', '3.7.2'

# gem 'bootstrap', '~> 5.1.3'

gem 'sprockets-rails'
gem 'turbo-rails'
gem 'stimulus-rails'
gem 'hotwire-rails'
gem 'importmap-rails'
gem 'jsbundling-rails'

gem 'jbuilder', '~> 2.5'
gem 'twilio-ruby', '5.72.1'
gem 'google-authenticator-rails'
gem 'friendly_id', '~> 5.2.0'
gem 'mysql2'

...

Error.

irb(main):003:0> ActionController::Base.new.render_to_string( template: "shared/file_template/test2" )
/Users/xxx/Documents/projects/rails-project/vendor/cache/ruby/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/xxx/Documents/projects/rails-project/vendor/cache/ruby/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"]
             ^^^^^^^^
@brunoprietog
Copy link
Contributor

I've opened #532 to solve this

@ryanckulp
Copy link

i have the same problem. perhaps &. isn't the best solution, but if there's a way to pass in a request object to render_to_string then we're all set.

@ryanckulp
Copy link

ryanckulp commented Jan 11, 2024

hey @brunoprietog , a simple solution i found is to simply pass in an empty Request object:

ac = ActionController::Base.new
ac.request = ActionDispatch::Request.new({})
ac.render_to_string(template: 'view/partial', locals: { variable: 'here' }, layout: 'application')

# => returns HTML string

seanpdoyle added a commit to seanpdoyle/turbo-rails that referenced this issue Apr 15, 2024
Follow-up to [hotwired#529][]
Closes [hotwired#613][]

Add test coverage to ensure that rendering outside of a request does not
raise `nil`-related `NoMethodError`.

Alongside a change to replace the `#turbo_frame_request_id` method's `&`
operator with a conditional, this commit also makes a similar change to
the `#turbo_native_app?` method, since it accesses the user agent in a
similar way.

[hotwired#529]: hotwired#529
[hotwired#613]: hotwired#613
seanpdoyle added a commit to seanpdoyle/turbo-rails that referenced this issue Apr 16, 2024
Follow-up to [hotwired#529][]
Closes [hotwired#613][]

Remove the `&` operator from the `Turbo::Frames::FrameRequest` concern,
since it isn't solving the original issues in the way that it intended.

Instead, add documentation to the `README.md` that highlights
`turbo-rails`'s compatibility with [ActionController::Renderer][].

[hotwired#529]: hotwired#529
[hotwired#613]: hotwired#613
[ActionController::Renderer]: https://api.rubyonrails.org/classes/ActionController/Renderer.html
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging a pull request may close this issue.

3 participants