1
class ApplicationViewComponent < ViewComponent::Base
  include ApplicationHelper
end

class FooComponent < ApplicationViewComponent 
end 

How can I include not only ApplicationHelper but also user created all helper files in view component?

1 Answers1

3

You don't include all the helpers.

Use the helpers proxy instead to access your Rails helpers.

class UserComponent < ViewComponent::Base
  def profile_icon
    helpers.icon :user
  end
end

And use delegate if you want to simplify the calls:

class UserComponent < ViewComponent::Base
  delegate :icon, to: :helpers

  def profile_icon
    icon :user
  end
end

Only include the helper modules when actually needed.

max
  • 96,212
  • 14
  • 104
  • 165
  • While you can at least in theory you could figure out all the constants in the `app/helpers` directory and then include each one that would be duplicating the functionality of ActionView and not a very good practice as it would confuse documention generation tools and other developers. – max Feb 16 '23 at 11:14