Ruby 之Gem kaminari


分页以及查询


GemFile

# 数据查询
gem 'pacecar', '1.5.3' # 给模型添加实用的scope
gem 'kaminari', '0.14.1' # 分页支持

Models

scope :like_filter, lambda { |query|
    if query.blank?
      { :conditions => ['TRUE'] }
    else
      {
        :conditions => [
          'login like ? OR name like ? OR email = ? OR id = ?',
          "%#{query}%", "%#{query}%", query, query
        ]
      }
    end
  }

Controller

@users = User.page(params[:page]).order('id DESC').like_filter(@query = params[:q])

index.html 


  = paginate @users


你可能感兴趣的:(Ruby,kaminari)