{"record":{"id":"b3b3a6c3f6c141da","repo":"ViewComponent/view_component","slug":"example-is-not-a-valid-preview-example","errorCode":null,"errorMessage":"#{example} is not a valid preview example","messagePattern":"#(.+?) is not a valid preview example","errorType":"http","errorClass":"AbstractController::ActionNotFound","httpStatus":404,"severity":"error","filePath":"lib/view_component/preview.rb","lineNumber":43,"sourceCode":"\n    def render_with_template(template: nil, locals: {})\n      {\n        template: template,\n        locals: locals\n      }\n    end\n\n    class << self\n      # Returns all component preview classes.\n      def all\n        __vc_load_previews\n\n        descendants\n      end\n\n      # Returns the arguments for rendering of the component in its layout\n      def render_args(example, params: {})\n        raise AbstractController::ActionNotFound, \"#{example} is not a valid preview example\" unless examples.include?(example.to_s)\n\n        example_params_names = instance_method(example).parameters.map(&:last)\n        provided_params = params.slice(*example_params_names).to_h.symbolize_keys\n        result = provided_params.empty? ? new.public_send(example) : new.public_send(example, **provided_params)\n        result ||= {}\n        result[:template] = preview_example_template_path(example) if result[:template].nil?\n        @layout = nil unless defined?(@layout)\n        result.merge(layout: @layout)\n      end\n\n      # Returns all of the available examples for the component preview.\n      def examples\n        public_instance_methods(false).map(&:to_s).sort\n      end\n\n      # Returns +true+ if the preview exists.\n      def exists?(preview)\n        all.any? { |p| p.preview_name == preview }","sourceCodeStart":25,"sourceCodeEnd":61,"githubUrl":"https://github.com/ViewComponent/view_component/blob/9f22c36fa7d7b44098a8ed97472e3b4674ae420f/lib/view_component/preview.rb#L25-L61","documentation":"Preview examples are public instance methods defined directly on a class inheriting ViewComponent::Preview; Preview.examples collects them via public_instance_methods(false), which excludes inherited and private methods. Preview.render_args raises AbstractController::ActionNotFound ('<example> is not a valid preview example') when the requested example name is not in that list. This is the error behind the preview routes (/rails/view_components/<preview>/<example>) when the example segment does not map to a method.","triggerScenarios":"Visiting /rails/view_components/user_preview/nonexistent with a typo'd or removed example name; defining the example as private or protected in the preview class; defining examples on a shared parent preview class (inherited methods are excluded by public_instance_methods(false)); calling render_args('default') from code after the method was renamed.","commonSituations":"Renaming or deleting a preview method while bookmarks or feature specs still point at the old URL; extracting shared examples into a base preview class during a refactor and having them disappear from examples; accidentally making example methods private.","solutions":["Define the example as a plain public instance method on the preview subclass: def my_example; render(MyComponent.new); end.","Fix the URL/spec to match an existing example name (names are the sorted method names; check via UserPreview.examples in the console).","If examples live in a shared parent class, redefine or delegate them in each concrete preview class, since inherited methods are not listed.","Remove private/protected from the example method so it shows up in .examples."],"exampleFix":"# before\nclass UserPreview < ViewComponent::Preview\n  private # makes the example invisible to .examples\n\n  def default\n    render(UserComponent.new(user: users(:one)))\n  end\nend\n\n# after\nclass UserPreview < ViewComponent::Preview\n  def default\n    render(UserComponent.new(user: users(:one)))\n  end\nend","handlingStrategy":"validation","validationCode":"example = \"default\"\nraise ArgumentError, \"#{example} not in #{UserPreview.examples}\" unless UserPreview.examples.include?(example)\n\nUserPreview.render_args(example)","typeGuard":null,"tryCatchPattern":"begin\n  UserPreview.render_args(example, params: params)\nrescue AbstractController::ActionNotFound\n  head :not_found # or redirect to the preview index\nend","preventionTips":["Keep preview examples public instance methods on the concrete preview class.","In preview controllers or specs, check Preview.examples.include?(name) before calling render_args.","When renaming examples, update links/specs and grep for the old URL segment."],"tags":["view-component","ruby-on-rails","previews","action-not-found","dev-tooling"],"backgroundTag":"action-not-found","analyzedSha":"9f22c36fa7d7b44098a8ed97472e3b4674ae420f","analyzedAt":"2026-08-23T10:20:47.008Z","schemaVersion":2},"datasetVersion":"2026-08-23T13:39:53.451Z"}