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

Raise error with tip when required ActionView context is missing #229

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

Conversation

andrewjanssen
Copy link

@andrewjanssen andrewjanssen commented Sep 18, 2024

I'm new to Phlex and am submitting this PR to a gotcha that got me. The docs explain that you can render a component by calling call on it. Because I had decided to demo Phlex by migrating an existing ERB partial to a Phlex component, I needed to render my new Phlex component from another ERB view. I did this with call. (Had I been demoing Phlex by implementing a new view from scratch, I would have copied the docs more directly, which would have led me to use render.) But using call in this way raised the following error:

ActionView::Template::Error (undefined method `default_url_options' for nil):

... from ERB lines such as:

<%= Components::MyComponent.new.call.html_safe %>

Since my ERB and Phlex components made no direct reference to default_url_options, I ended up debugging for a while before realising that the ActionView context is set when you call render, so for components which include Rails helpers in this repo, you probably have to render them with render, not call. This PR shows a helpful error to other people who will make the same mistake:

👋 Please use `render` to render Components::MyComponent instances (instead
of my_component_instance.call). Using `render` allows the component to
receive the ActionView context, which is required by Phlex helpers it includes.

Let me know if you'd like me to change this PR in any way. Thanks for Phlex! Really excited about it.

@Vagab
Copy link
Contributor

Vagab commented Oct 2, 2024

@joeldrapper I made almost the same pr here. Let me know if I should close that one

@joeldrapper joeldrapper added this to the 2.0 milestone Oct 3, 2024
@andrewjanssen
Copy link
Author

Resolves #220.

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.

3 participants