将分页添加到以下文本:

时间:2012-04-02 15:06:40

标签: ruby-on-rails controller pagination

我目前在用户桌面上有分页,因为我只为用户提供了一个搜索功能。

但我现在希望为我的游戏桌添加分页,但不知道如何实现它,因为我有5个游戏搜索功能。我想知道如何将分页添加到以下控制器方法中。

    def index
        @games = Game.gamsearch(params[:gamsearch])
        @games = Game.consearch(params[:consearch]) if params[:consearch].present?
        @games = Game.gensearch(params[:gensearch]) if params[:gensearch].present?
        @games = Game.where("game_name LIKE ?", "#{params[:game_name]}%") if params[:game_name].present?
        @games = Game.where("console = ?", params[:console]) if params[:console].present?

    end

1 个答案:

答案 0 :(得分:0)

我认为你应该使用在你的Game类中返回ActiveRecord关系的范围或方法并将它们链接起来(我想你想用AND运算符组合condidtions):

class Games
  scope gamsearch, lambda { |search_param| your_conditions }
  scope consearch, lambda { |search_param| your_conditions }
  scope gensearch, lambda { |search_param| your_conditions }
  scope game_name, lambda { |game_name_param| where("game_name LIKE ?", "#{game_name_param}%") }
  scope console, lambda { |console_param| where("console = ?", console_param) }
end


def index
  scope = Game.gamsearch(params[:gamsearch])
  scope = scope.consearch(params[:consearch]) if params[:consearch].present?
  scope = scope.gensearch(params[:gensearch]) if params[:gensearch].present?
  scope = scope.game_name(params[:game_name]) if params[:game_name].present?
  scope = scope.console(params[:console]) if params[:console].present?
  @games = scope.paginate
end