我正在使用will_paginate进行分页,迄今为止一直运行良好,除了这一件事.
例如,如果我尝试分页
class User < ActiveRecord::Base named_scope :scope,lambda { etc } end User.scope.paginate({:page => params[:page],:per_page => 10})
解决方法
Lowgain,klew的版本应该开箱即用.在你的版本你应该写:
User.scope.paginate :page => params[:page],:per_page => 10
我更喜欢分页的另一种方法.它允许控制器更清洁,并在模型级别封装分页,例如:
class Property < ActiveRecord::Base named_scope :all_properties,lambda {{ :order => "name asc" }} def self.admin_properties(page = 1) self.all_properties.paginate(:page => page,:per_page => Settings.admin.per_page) end end
而在一个控制器中,相当清晰的代码:
class Admin::PropertiesController < Admin::AdminController def index @properties = Property.admin_properties(params[:page]) end end
p.s:Settings.admin.per_page – 这是Searchlogic设置.