{"record":{"id":"a04411a9d7f890e4","repo":"activerecord-hackery/ransack","slug":"attribute-select-must-be-called-inside-a-search-fo","errorCode":null,"errorMessage":"attribute_select must be called inside a search FormBuilder!","messagePattern":"attribute_select must be called inside a search FormBuilder!","errorType":"exception","errorClass":"ArgumentError","httpStatus":null,"severity":"error","filePath":"lib/ransack/helpers/form_builder.rb","lineNumber":51,"sourceCode":"        i18n = options[:i18n] || {}\n        text ||= object.translate(\n          method, i18n.reverse_merge(include_associations: true)\n          ) if object.respond_to? :translate\n        super(method, text, options, &block)\n      end\n\n      def submit(value = nil, options = {})\n        value, options = nil, value if value.is_a?(Hash)\n        value ||= Translate.word(:search).titleize\n        super(value, options)\n      end\n\n      def attribute_select(options = nil, html_options = nil, action = nil)\n        options ||= {}\n        html_options ||= {}\n        action ||= Constants::SEARCH\n        default = options.delete(:default)\n        raise ArgumentError, formbuilder_error_message(\n          \"#{action}_select\") unless object.respond_to?(:context)\n        options[:include_blank] = true unless options.has_key?(:include_blank)\n        bases = [''.freeze].freeze + association_array(options[:associations])\n        if bases.size > 1\n          collection = attribute_collection_for_bases(action, bases)\n          object.name ||= default if can_use_default?(\n            default, :name, mapped_values(collection.flatten(2))\n            )\n          template_grouped_collection_select(collection, options, html_options)\n        else\n          collection = collection_for_base(action, bases.first)\n          object.name ||= default if can_use_default?(\n            default, :name, mapped_values(collection)\n            )\n          template_collection_select(:name, collection, options, html_options)\n        end\n      end\n","sourceCodeStart":33,"sourceCodeEnd":69,"githubUrl":"https://github.com/activerecord-hackery/ransack/blob/e82f6bab3956c5597e7debbefa218d9e94e58ceb/lib/ransack/helpers/form_builder.rb#L33-L69","documentation":"Ransack's FormBuilder#attribute_select (and sort_select, which delegates to it) requires the form's underlying object to be a Ransack::Search — detected by responding to #context. If you use these helpers inside a plain form_for(@record) form, the object is an AR model that does not respond to :context, and this ArgumentError is raised.","triggerScenarios":"<%= form_for @article do |f| %> <%= f.attribute_select %> — attribute_select, f.sort_select, or any 'search_select'/'sort_select' action variant called on a builder bound to a model instance or relation instead of a Ransack::Search.","commonSituations":"Copy-pasting a Ransack view snippet into an existing form_for block; forgetting to build @q in the controller; using simple_form_for/form_with with a record instead of search_form_for with a search object.","solutions":["Build a search object in the controller: @q = Article.ransack(params[:q])","Use search_form_for @q do |f| (Ransack's helper) so the builder object is the search, then f.attribute_select works","If the form must stay a plain form_for, replace attribute_select with your own collection_select over Article.ransackable_attributes"],"exampleFix":"# controller\nbefore: @article = Article.new\nafter:  @q = Article.ransack(params[:q])\n\n# view\n<!-- before -->\n<%= form_for @article do |f| %>\n  <%= f.attribute_select %>\n<% end %>\n\n<!-- after -->\n<%= search_form_for @q do |f| %>\n  <%= f.attribute_select %>\n<% end %>","handlingStrategy":"type-guard","validationCode":null,"typeGuard":"def search_form_builder?(builder)\n  builder.object.respond_to?(:context) # true only for Ransack::Search objects\nend","tryCatchPattern":null,"preventionTips":["Only call attribute_select/sort_select inside search_form_for blocks","Set a controller before_action that assigns @q = Model.ransack(params[:q]) for every action rendering a search form","Smoke-test views that mix plain form_for with copied Ransack snippets — they fail only at render time"],"tags":["ruby","rails","ransack","formbuilder","actionview","views"],"backgroundTag":"form-builder-wrong-object","analyzedSha":"e82f6bab3956c5597e7debbefa218d9e94e58ceb","analyzedAt":"2026-08-21T19:30:23.639Z","schemaVersion":2},"datasetVersion":"2026-08-21T23:17:16.201Z"}