2013-04-30 73 views
1

我跟隨railscast 111進行高級搜索。我得到一個未定義的局部變量或方法`find_users'。我已經按照線路排隊。不知道如何解決這個問題,因爲我做了搜索後出現錯誤。它在搜索#show中指向NoMethodError。任何幫助,將不勝感激。find_users的未定義局部變量

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 

回答

2

在你search.rb必須添加方法find_users,像這樣:

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

然後您還必須包含所有方法conditions並指定它們。請遵循railscast,它非常明確。