1

我正在使用 sinatra、sequql 和 postgresql。

到目前为止,我有:

require 'will_paginate'
require 'will_paginate/sequel'
require 'will_paginate/collection'
require 'will_paginate/version'
require 'sequel/extensions/pagination'

红宝石代码是:

get '/candidate' do
     @items = DB[:candidates].order(:id).extension(:pagination).paginate(1, 10)
     erb :candidate
end

在视图中:

<%= will_paginate @items %>

我已经尝试了查询的多种变体以使分页正常工作,但都以失败告终。以下根据请求生成 10 行的页面,但是当我单击第 2 页时返回错误消息。

我已经尝试过包含和不包含续集扩展分页,并且“will_paginate”的创建者坚持认为他们无论如何都可以一起工作。

此外,根据其他人的建议,我尝试过:

get '/candidate' do
  @items = DB[:candidates].order(:id).paginate(:page => params["page"], :per_page => 10)
  erb :candidate
end

get '/candidate' do
  page = params.fetch "page", 1
  per_page = params.fetch "per_page", 10
  @items = DB[:candidates].order(:id).paginate(:page => page, :per_page => per_page)
  erb :candidate
end

两者都不起作用。我收到一个错误,基本上说“参数数量错误(1 对 2)”。

有人有使用 SEQUEL 成功分页结果的经验吗?感谢所有帮助。

4

1 回答 1

1

我有同样的问题。paginate 方法不采用 :page 和 :per_page 参数。https://github.com/jeremyevans/sequel/blob/master/lib/sequel/extensions/pagination.rb

出于某种原因, fetch 也不适合我。我最终使用:

page = params[:page].to_i
page = 1 unless page > 0
@notes = DB[:notes].extension(:pagination).paginate(page, 20)
于 2013-12-08T21:30:41.183 回答