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

ViewComponentHelpers.allow_rails_helpers doesn't work #665

Closed
joemasilotti opened this issue Dec 4, 2022 · 3 comments · Fixed by #687
Closed

ViewComponentHelpers.allow_rails_helpers doesn't work #665

joemasilotti opened this issue Dec 4, 2022 · 3 comments · Fixed by #687
Labels
bug Something isn't working

Comments

@joemasilotti
Copy link
Contributor

When using Rails helpers the :tag option only works when the server automatically reloads. But not on first launch.

Bridgetown Version:
bridgetown 1.1.0 "Belmont"

To Reproduce

  1. Follow the official ViewComponent tutorial
  2. Enable :tag helper from Rails
  3. Use the tag helper in a component

Current behavior
If starting the app cold it crashes with the error produced below.

Note that if the tag helper is not used, then the server is started, then the tag helper is used it works as expected.

Expected behavior
The tag helper should not raise an error.

Computing environment (please complete the following information):

  • OS: [e.g. iOS]
  • Safari
  • Version 16.1 (18614.2.9.1.12)
  • ruby 3.1.2p20 (2022-04-12 revision 4491bb740a) [x86_64-darwin21]

Additional context

[Bridgetown]           Starting: Bridgetown v1.1.0 (codename "Belmont")
[Server] * Puma version: 5.6.5 (ruby 3.1.2-p20) ("Birdie's Version")
[Server] * PID: 99909
[Frontend] touch frontend/styles/jit-refresh.css
[Frontend] yarn run esbuild-dev
[Frontend] yarn run v1.22.19
[Frontend] $ node esbuild.config.js --watch
[Frontend] esbuild: frontend bundling started...
[Frontend] esbuild: frontend bundling complete!
[Frontend] esbuild: entrypoints processed:
[Frontend]          - index.EVFXU2NE.js: 78B
[Frontend]          - index.ETTOPBPI.css: 16.87KB
[Bridgetown]        Environment: development
[Bridgetown]             Source: /Users/joemasilotti/workspace/projects/masilotti.com/bridgetown/src
[Bridgetown]        Destination: /Users/joemasilotti/workspace/projects/masilotti.com/bridgetown/output
[Bridgetown]     Custom Plugins: /Users/joemasilotti/workspace/projects/masilotti.com/bridgetown/plugins
[Bridgetown]         Generating…
[Bridgetown]         Pagination: disabled. Enable in site config with pagination:\n enabled: true
[Bridgetown]   Conversion error: Bridgetown::Converters::ERBTemplates encountered an error while converting `index.erb'
[Bridgetown] Stopping auxiliary processes...
[Bridgetown]   Exception raised: NameError
[Bridgetown] undefined local variable or method `tag' for #<SiteComponent:0x00000001147bdff8 @view_context=#<Bridgetown::ERBView layout=default resource=index.erb>, @__vc_original_view_context=#<Bridgetown::ERBView layout=default resource=index.erb>, @output_buffer="", @lookup_context={"variants"=>[]}, @view_renderer=nil, @view_flow=nil, @virtual_path="/Users/joemasilotti/workspace/projects/masilotti.com/bridgetown/src/_components/site_component", @__vc_variant=nil, @current_template=nil, @__vc_content_evaluated=false, @__vc_render_in_block=nil, @helpers=#<Bridgetown::RubyTemplateView::Helpers:0x00000001147bd788 @view=#<SiteComponent:0x00000001147bdff8 ...>, @site=#<Bridgetown::Site "title"=>"Masilotti.com", "tagline"=>"Joe Masilotti’s blog on Ruby on Rails, iOS, Turbo Native, and automated testing.", "email"=>"joe@masilotti.com", "description"=>"A blog from Joe Masilotti, an independent developer who's passionate about clean, testable code.", "twitter"=>"joemasilotti", "github"=>"joemasilotti">, @context=#<struct Bridgetown::RubyTemplateView::Helpers::Context registers={:site=>#<Bridgetown::Site "title"=>"Masilotti.com", "tagline"=>"Joe Masilotti’s blog on Ruby on Rails, iOS, Turbo Native, and automated testing.", "email"=>"joe@masilotti.com", "description"=>"A blog from Joe Masilotti, an independent developer who's passionate about clean, testable code.", "twitter"=>"joemasilotti", "github"=>"joemasilotti">}>>>
Did you mean?  tap
[Bridgetown]                  1: /Users/joemasilotti/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/bridgetown-view-component-1.0.0/lib/bridgetown-view-component/bridgetown/view_component_helpers.rb:52:in `method_missing'
[Bridgetown]                  2: /Users/joemasilotti/workspace/projects/masilotti.com/bridgetown/src/_components/site_component.rb:7:in `call'
[Bridgetown]                  3: /Users/joemasilotti/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/view_component-2.74.1/lib/view_component/compiler.rb:112:in `render_template_for'
[Bridgetown]                  4: /Users/joemasilotti/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/view_component-2.74.1/lib/view_component/base.rb:131:in `render_in'
[Bridgetown]                  5: /Users/joemasilotti/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/bridgetown-core-1.1.0/lib/bridgetown-core/ruby_template_view.rb:31:in `render'
[Bridgetown]          Backtrace: Use the --trace option for complete information.
@joemasilotti joemasilotti added the bug Something isn't working label Dec 4, 2022
@joemasilotti
Copy link
Contributor Author

Turns out you need to allow the Rails helpers before including the Bridgetown helpers.

This works:

class SiteComponent < ViewComponent::Base
  Bridgetown::ViewComponentHelpers.allow_rails_helpers :tag
  include Bridgetown::ViewComponentHelpers
end

This does not work:

class SiteComponent < ViewComponent::Base
  include Bridgetown::ViewComponentHelpers
  Bridgetown::ViewComponentHelpers.allow_rails_helpers :tag
end

@jaredcwhite
Copy link
Member

@joemasilotti Ah, that makes sense. Maybe there's a way we can document this better or otherwise make it more obvious how it works?

@joemasilotti
Copy link
Contributor Author

joemasilotti commented Dec 31, 2022

Done! #687.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants