{"record":{"id":"bc6248637283d21d","repo":"ViewComponent/view_component","slug":"helpers-can-t-be-used-before-rendering-as-it-de","errorCode":null,"errorMessage":"`#helpers` can't be used before rendering as it depends on the view context that only exists once a ViewComponent is passed to the Rails render pipeline.\n\nIt's sometimes possible to fix this issue by moving code dependent on `#helpers` to a [`#before_render` method](https://viewcomponent.org/api.html#before_render--void).","messagePattern":"`#helpers` can't be used before rendering as it depends on the view context that only exists once a ViewComponent is passed to the Rails render pipeline\\.\n\nIt's sometimes possible to fix this issue by moving code dependent on `#helpers` to a \\[`#before_render` method\\]\\(https://viewcomponent\\.org/api\\.html#before_render--void\\)\\.","errorType":"exception","errorClass":"ViewComponent::HelpersCalledBeforeRenderError","httpStatus":null,"severity":"error","filePath":"lib/view_component/base.rb","lineNumber":326,"sourceCode":"      @output_buffer = old_output_buffer\n    end\n\n    # The current controller. Use sparingly as doing so introduces coupling\n    # that inhibits encapsulation & reuse, often making testing difficult.\n    #\n    # @return [ActionController::Base]\n    def controller\n      raise ControllerCalledBeforeRenderError if view_context.nil?\n\n      @__vc_controller ||= view_context.controller\n    end\n\n    # A proxy through which to access helpers. Use sparingly as doing so introduces\n    # coupling that inhibits encapsulation & reuse, often making testing difficult.\n    #\n    # @return [ActionView::Base]\n    def helpers\n      raise HelpersCalledBeforeRenderError if view_context.nil?\n\n      # Attempt to re-use the original view_context passed to the first\n      # component rendered in the rendering pipeline. This prevents the\n      # instantiation of a new view_context via `controller.view_context` which\n      # always returns a new instance of the view context class.\n      #\n      # This allows ivars to remain persisted when using the same helper via\n      # `helpers` across multiple components and partials.\n      @__vc_helpers ||= __vc_original_view_context || controller.view_context\n    end\n\n    if defined?(Rails.env) && (::Rails.env.development? || ::Rails.env.test?)\n      # @private\n      def method_missing(method_name, *args) # rubocop:disable Style/MissingRespondToMissing\n        super\n      rescue => e # rubocop:disable Style/RescueStandardError\n        e.set_backtrace e.backtrace.tap(&:shift)\n        raise e, <<~MESSAGE.chomp if view_context && e.is_a?(NameError) && helpers.respond_to?(method_name)","sourceCodeStart":308,"sourceCodeEnd":344,"githubUrl":"https://github.com/ViewComponent/view_component/blob/9f22c36fa7d7b44098a8ed97472e3b4674ae420f/lib/view_component/base.rb#L308-L344","documentation":"Error \"`#helpers` can't be used before rendering as it depends on the view context that only exists once a ViewComponent is passed to the Rails render pipeline.\n\nIt's sometimes possible to fix this issue by moving code dependent on `#helpers` to a [`#before_render` method](https://viewcomponent.org/api.html#before_render--void).\" thrown in ViewComponent/view_component.","triggerScenarios":"Thrown at lib/view_component/base.rb:326 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Move code that calls `#helpers` into a `#before_render` method or the template, where the view context is available.","Alternatively include the needed helper module directly into the component class and call it without going through `#helpers`."],"exampleFix":null,"handlingStrategy":null,"validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"9f22c36fa7d7b44098a8ed97472e3b4674ae420f","analyzedAt":"2026-08-23T10:20:47.008Z","schemaVersion":2},"datasetVersion":"2026-08-23T13:39:53.451Z"}