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

[Rails] Explain how to render without a view context #54

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

andrewjanssen
Copy link
Contributor

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.

@andrewjanssen andrewjanssen force-pushed the rails-rendering-out-of-context branch 4 times, most recently from 1ac002c to 2bafbad Compare December 4, 2024 19:41
@andrewjanssen andrewjanssen force-pushed the rails-rendering-out-of-context branch from 2bafbad to 48ddac8 Compare December 4, 2024 19:41
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
Copy link
Collaborator

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.

@joeldrapper
Copy link
Collaborator

@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.

@andrewjanssen
Copy link
Contributor Author

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.

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