{"record":{"id":"38e96c7a33669ec3","repo":"ViewComponent/view_component","slug":"controller-can-t-be-used-before-rendering-as-i","errorCode":null,"errorMessage":"`#controller` 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 `#controller` to a [`#before_render` method](https://viewcomponent.org/api.html#before_render--void).","messagePattern":"`#controller` 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 `#controller` to a \\[`#before_render` method\\]\\(https://viewcomponent\\.org/api\\.html#before_render--void\\)\\.","errorType":"exception","errorClass":"ViewComponent::ControllerCalledBeforeRenderError","httpStatus":null,"severity":"error","filePath":"lib/view_component/base.rb","lineNumber":316,"sourceCode":"    # (whose _run allocated a fresh OutputBuffer on the view context), the\n    # component's stale @output_buffer would capture from the wrong buffer.\n    # Temporarily switching to the view context's buffer keeps both in sync.\n    #\n    # @private\n    def capture(...)\n      old_output_buffer = @output_buffer\n      @output_buffer = view_context.output_buffer if view_context\n      super\n    ensure\n      @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.","sourceCodeStart":298,"sourceCodeEnd":334,"githubUrl":"https://github.com/ViewComponent/view_component/blob/9f22c36fa7d7b44098a8ed97472e3b4674ae420f/lib/view_component/base.rb#L298-L334","documentation":"Error \"`#controller` 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 `#controller` 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:316 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Move code that calls `#controller` into a `#before_render` method (or the template), which only runs once the view context exists.","If controller data is needed at initialization time, pass it explicitly into the component's initializer instead of reading it via `#controller`."],"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"}