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

Support rendering partials from controller renderers #96

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

seanpdoyle
Copy link
Collaborator

Gems like turbo-rails utilize the
ActionController::Renderer.renderer class method to render templates and partials outside the context of a request-response cycle.

Prior to this bugfix, partials rendered by controller renderers raised errors like the following:

Error:
RendererTest#test_TestController.render_partial_that_declares_helper_methods:
ActionView::Template::Error: undefined method `helpers' for nil:NilClass
    bullet-train-co/nice_partials/test/fixtures/_partial_with_helpers.html.erb:2:in `_fixtures__partial_with_helpers_html_erb__3099800394007487670_4260'

Gems like [turbo-rails][] utilize the
[ActionController::Renderer.renderer][] class method to render templates
and partials outside the context of a request-response cycle.

Prior to this bugfix, partials rendered by controller renderers raised
errors like the following:

```
Error:
RendererTest#test_TestController.render_partial_that_declares_helper_methods:
ActionView::Template::Error: undefined method `helpers' for nil:NilClass
    bullet-train-co/nice_partials/test/fixtures/_partial_with_helpers.html.erb:2:in `_fixtures__partial_with_helpers_html_erb__3099800394007487670_4260'
```

[ActionController::Renderer.renderer]: https://edgeapi.rubyonrails.org/classes/ActionController/Renderer.html#method-i-render
[turbo-rails]: https://github.com/hotwired/turbo-rails/blob/e44b6a98a77a0a2cc927f986a67517d73c4c9246/app/channels/turbo/streams/broadcasts.rb#L87
@@ -55,6 +55,14 @@ class RendererTest < NicePartials::Test
assert_css "p", text: "UPCASED"
end

test "TestController.render partial that declares helper methods" do
TestController.renderer.render partial: "partial_with_helpers" do
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kaspth currently, this PR is aspirational. This test fails with the stack trace provided in the PR description. I haven't had much success troubleshooting, but I have the sense that it's related to how the monkey patch is (or isn't) being applied to the ActionView::Base instance available to renderer.render calls.

@kaspth
Copy link
Contributor

kaspth commented Nov 27, 2023

@seanpdoyle I forgot to say thank you for this! I haven't worked on Nice Partials in a bit, and I don't know when I'll get to this yet. But yeah, it would be cool to fix this 😄

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

Successfully merging this pull request may close these issues.

2 participants