how is the rails way include all user custom helper files in view component

701 Views Asked by At
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

There are 1 best solutions below

1
max On

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.