find_users的未定义局部变量

时间:2013-04-30 16:15:10

标签: ruby-on-rails advanced-search

我正在关注railscast 111进行高级搜索。我得到一个未定义的局部变量或方法`find_users'。我一直跟着它排队。不确定如何解决这个问题,因为我在搜索后得到了错误。它指向搜索#show中的NoMethodError。任何帮助将不胜感激。

show.html:

 

搜索控制器:

  def new
    @search = Search.new
  end

  def create
    @search = Search.new(params[:search])
    if @search.save
      redirect_to @search
    else
      render 'new'
    end
  end

  def show
    @search = Search.find(params[:id])
    @users = @search.users
  end
end

search.rb:

  attr_accessible :age, :children, :ethnicity, :gender, :religion, :zip_code

  def users
    @users ||= find_users

    private

      def find_users
        users = User.order(:id)
        users = users.where(gender: gender) if gender
        users = users.where(zip_code: zip_code) if zip_code
        users = users.where(children: children) if children
        users = users.where(religion: religion) if religion
        users = users.where(ethnicity: ethnicity) if ethnicity
        users
      end
    end
    end

1 个答案:

答案 0 :(得分:2)

search.rb中,您必须添加方法find_users,如下所示:

def find_users
  User.find(:all, :conditions => conditions)
end

然后,您还必须包含所有方法conditions并指定它们。请关注railscast,它非常明确。