自定义will_paginate按钮

时间:2011-07-20 05:43:17

标签: ruby-on-rails ruby-on-rails-3 will-paginate

有没有办法自定义will_paginate控制按钮来添加更多按钮,如FIRST和LAST,而不仅仅是上一个和下一个按钮。谢谢。

2 个答案:

答案 0 :(得分:3)

第一页非常简单:

link_to 'First Page', :action => :whatever, :page => 1

最后一页有点棘手,在你的模型类中添加:

class << self
  # number of records per page, from the will_paginate docs
  def per_page
    20
  end

  # takes a hash of finder conditions and returns a page number
  # returns 1 if nothing was found, as not to break pagination by passing page=0
  def last_page_number(conditions=nil)
    total = count :all, :conditions => conditions
    [((total - 1) / per_page) + 1, 1].max
  end
end 

现在你可以这样做:

link_to 'Last page', :action => :whatever, :page => Model.last_page_number

答案 1 :(得分:1)

假设您正在对@users进行分页,然后将这些链接放在您的视图中

<%= link_to 'First Page', :action => 'index', :page => 1 %>
<%= will_paginate @users %>
<%= link_to 'Last Page', :action => 'index', :page => @users.page_count %>