-
-
Notifications
You must be signed in to change notification settings - Fork 30
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
[Rails] Explain how to render without a view context #54
base: main
Are you sure you want to change the base?
[Rails] Explain how to render without a view context #54
Conversation
1ac002c
to
2bafbad
Compare
2bafbad
to
48ddac8
Compare
fake_rack_request = Rack::MockRequest.env_for("http://localhost", method: :get) | ||
controller.request = ActionDispatch::Request.new(fake_rack_request) | ||
html = controller.render_to_string(component, layout: false) | ||
HelloMailer.with(html:).hello_email.deliver_now |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unless you need to access Rails’ view helpers, another way to render to HTML is to call it. html = component.call
I’m not sure how Turbo handles controller-less rendering when you pass a renderable:
(see above) but I wonder if looking into this might reveal a cleaner way to get a rendering context without a controller.
@andrewjanssen thanks for the PR. I left a couple of comments. I’m going to do a little research to see if there’s any way we can improve on this. Totally agree we need to cover it in the docs. |
Co-authored-by: Joel Drapper <joel@drapper.me> Signed-off-by: Janssen <m.andrew.janssen@gmail.com>
You're welcome! Yes, it would be nice to avoid this fake object approach. Hard for me to know if this complexity could be pushed into phlex-rails or if that would be detrimental. |
I want to migrate an existing project to Phlex. But when moving my AR-callback-driven Turbo Stream renders, I hit exceptions and confusement. This PR will help others avoid the investigation that I did to learn how to render from AR callbacks, background jobs, etc.