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

ViewComponent::Base.config.view_component_path nil error #1741

Closed
geekdreamzz opened this issue May 5, 2023 · 16 comments · Fixed by #1774
Closed

ViewComponent::Base.config.view_component_path nil error #1741

geekdreamzz opened this issue May 5, 2023 · 16 comments · Fixed by #1774

Comments

@geekdreamzz
Copy link

Hi - I recently updated to 3.0.0 and suddenly started getting this error on all my components:

ActionView::Template::Error (no implicit conversion of nil into String)

When I go into the stack trace I see it errors out at https://github.com/ViewComponent/view_component/blob/main/lib/view_component/base.rb#L452
Regexp.quote(ViewComponent::Base.config.view_component_path)

I'm not sure what ViewComponent::Base.config.view_component_path is for? but it's nil for me and this Regexp.quote method throws the error.

To get back up and running I just set ViewComponent::Base.config.view_component_path = '' and I'm good, but it's clearly a workaround.

TBH, I'm not sure if I'm using the gem the intended way but I essentially create my own classes that inherit from ViewComponent::Base. I could send a PR to account for nil ViewComponent::Base.config.view_component_path but it would just mask the issue. I'm a bit out of my wheelhouse trying to debug the underlying issue, I'm not able to follow what the comments here imply:

# Removes the first part of the path and the extension. child.virtual_path = child.source_location.gsub( /(.*#{Regexp.quote(ViewComponent::Base.config.view_component_path)})|(\.rb)/, "" )

I'd be happy to contribute to the gem if you want to give some feedback on what ViewComponent::Base.config.view_component_path is for and what child.virtual_path is designed for.

My base class that inherits from ViewComponent::Base also has a config so it's prob something dumb on my end. You could also just disregard this but figured I share the error in case it happens to anyone else.

@reeganviljoen
Copy link
Collaborator

reeganviljoen commented May 6, 2023

@geekdreamzz so ViewComponent::Base.config.view_component_path is used to specify where to locate components if the default app/componets isn't used

https://github.com/ViewComponent/view_component/blob/main/lib/view_component/base.rb#L289 explains why it is nil in your case, hear is the line for reference:

Defaults to `nil`. If this is falsy, `app/components` is used.

@reeganviljoen
Copy link
Collaborator

If you want to use a parent component generate a component with this bin/rails generate component Example title content --parent MyBaseComponent

@GyozaGuy
Copy link

GyozaGuy commented May 9, 2023

I'm seeing this error as well as of today after updating my gems.

@reeganviljoen your explanation makes sense, but the app won't even run without manually setting the config.

@Spone
Copy link
Collaborator

Spone commented May 9, 2023

@boardfish mind looking into this?

@reeganviljoen
Copy link
Collaborator

@thank you @GyozaGuy I see

@reeganviljoen
Copy link
Collaborator

@GyozaGuy @Spone @boardfish this pr seems to have caused the issue #1659

@donapieppo
Copy link
Contributor

You may try the pull request at
#1738
since it looks similar to the issue #1565

@reeganviljoen
Copy link
Collaborator

reeganviljoen commented May 23, 2023

@donapieppo this pr #1659 moved all config defaults out of the base class so any path configs dont work in 3.0 unless you specify them yourself

@boardfish
Copy link
Collaborator

@geekdreamzz @GyozaGuy #1774 may fix your issue. Could you give it a shot by updating your Gemfile to point at that branch of this repo? Thanks!

@GyozaGuy
Copy link

I'm no longer getting the original error, but this new one is now happening:

/home/gyozaguy/.rbenv/versions/3.2.0/lib/ruby/gems/3.2.0/gems/view_component-3.2.0/lib/view_component/preview.rb:7:in `<class:Preview>': undefined method `routes' for nil:NilClass (NoMethodError)

    include Rails.application.routes.url_helpers
                             ^^^^^^^

@boardfish
Copy link
Collaborator

Thanks! This looks familiar – I got the same thing when I reverted the original PR that fixed the load order, so that sort of signalled this. I'll update that PR in a bit and let you know in this thread.

It's probably best just to get this fixed for now, but if there's a way we can emulate the inclusion of the gem in an environment where Rails hasn't loaded such that we prevent this from breaking in future, I'm interested in getting that sorted. I hope we can continue to allow folks to extend off ViewComponent via gems.

@boardfish
Copy link
Collaborator

@GyozaGuy It's now been updated to only include the URL helpers if the Rails app has been loaded. You're welcome to give that a try whenever you're ready!

@GyozaGuy
Copy link

Yep that's working now! Thanks for your work on this!

@boardfish
Copy link
Collaborator

Great to hear it, and no problem!

@reeganviljoen
Copy link
Collaborator

@geekdreamzz do you have any updates on this issue

@geekdreamzz
Copy link
Author

@reeganviljoen @boardfish sorry for the late reply. it resolves my issues too! thanks

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 a pull request may close this issue.

6 participants