Tire search and will_paginate - undefined method `offset'

浪子不回头ぞ 提交于 2020-01-05 07:45:06

问题


After headaches with ThinkingSphinx and Solr/Sunspot, we're trying out ElasticSearch and Tire as our search back-end - but I've hit a problem.

Here's my search command in the controller:

@results = Item.search params[:search], :page => ( params[:page] || 1 ), :per_page => 20

And this is the problem section of the view:

<%= page_entries_info @results %>

The error message I'm getting is

undefined method `offset' for #<Tire::Results::Collection:0xa3f01b0>

but only when there is more than one page's worth of results. If there are less than 20 items returned, then they get shown fine.

The only similar reported issue I could find elsewhere was solved by passing the :page and :per_page parameters into the search function, but I'm already doing that, to no avail.


回答1:


Tire has a Pagination module but it doesn't define offset. You could file an issue with them to add it, but in the meantime you can monkeypatch it in your app:

Tire::Results::Pagination.module_eval do
  def offset
    (@options[:per_page] || @options[:size] || 10 ).to_i * (current_page - 1)
  end
end



回答2:


in my testapp, results are paginated just fine, with will_paginate 3.0 and tire 0.3. I wasn't aware will_paginate needed the offset method.

I've added it, however, copying over the "lint" test from will_paginate specs: https://github.com/karmi/tire/commit/e0e7730. Should be part of the next release.



来源:https://stackoverflow.com/questions/7306623/tire-search-and-will-paginate-undefined-method-offset

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!