我正在通过教程学习rails高级搜索,用户可以根据该类别查找帖子。但不知何故,我的高级页面给了我这个错误:
搜索中的ArgumentError #new Showing app / views / searches / new.html.haml第10行 提出:
错误的参数数量(3为1..2)
我收到错误的行是:
= s.text_field :category, options_for_select(@categories), :include_blank => true
以上文件是new.html.haml(第10行)。这是搜索controller.rb,
我认为@categories
方法中的new
不正确。
class SearchesController < ApplicationController
def new
@search = Search.new
@categories = Idea.uniq.pluck(:category)
end
def create
@search = Search.create(search_params)
redirect_to @search
end
def show
@search = Search.find(params[:idea_id])
end
private
def search_params
params.require(:search).permit(:keywords, :category)
end
end
我的model.rb
class Search < ActiveRecord::Base
def search_ideas
ideas = Idea.all
ideas = ideas.where(["title LIKE ?","%#{keywords}%"]) if keywords.present?
ideas = ideas.where(["category LIKE ?","%#{category}%"]) if category.present?
return ideas
end
end
我尝试使用collection_select
代替s.text_field
,但它仍然给我这个错误。
答案 0 :(得分:2)
尝试使用s.select
,如下所示:
= s.select :category, options_for_select(@categories), :include_blank => true
答案 1 :(得分:1)
首先参考documentation。
select(object,method,choices = nil,options = {},html_options = {},&amp; block)
因为它有两个哈希选项,所以你需要在options
之前指定html_options
s.select :category, options_for_select(@categories), {}, {include_blank: true}