将 searchlogic 与 will_paginate 结合使用
编辑 看起来我已经弄清楚了 - 我必须在从 Searchlogic 调用 all
之后调用分页。
我正在尝试使用这两个工具来使用户能够搜索联系人并返回分页列表(如果不输入任何搜索条件,则返回整个分页列表)。然而,我不确定将它们链接在一起的正确方法,而我正在尝试的却是错误。
这是我的控制器:
class ContactsController < ApplicationController
def index
@search = Contact.search(params[:search]).paginate(:page => params[:page])
@contacts, @contacts_count = @search.all, @search.count
end
end
这给了我错误 Undefined method 'all' for WillPaginate
。删除所有内容会给我一个错误,因为视图正在寻找包含 20 次“contact”一词的路径(例如 contact_contact_contact..._path
),大概是因为默认的“每页”为 20 ?
我做错了什么 我想在这个页面上进行搜索、排序和分页。
EDIT Looks like I figured it out - I had to call paginate after the call to all
from Searchlogic.
I'm trying to use both of these tools to enable users to search contacts and return a paginated list (or the entire paginated list if they don't enter any search criteria). However I'm unsure of the proper way to chain them together, and what I'm trying is giving me errors.
Here is my controller:
class ContactsController < ApplicationController
def index
@search = Contact.search(params[:search]).paginate(:page => params[:page])
@contacts, @contacts_count = @search.all, @search.count
end
end
This gives me the error Undefined method 'all' for WillPaginate
. Removing the all gives me an error because the view is looking for a path that has the word "contact" 20 times (e.g. contact_contact_contact..._path
), presumably because the default "per page" is 20.
What am I doing wrong? I would like to have searching, ordering and pagination all on this page.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我也被这个搞糊涂了。您想要执行以下操作:
在您的视图中,只需调用 @contacts.total_entries 即可获取总计数(will_paginate 会自动添加该计数)。
一旦你调用 .paginate 它就会触发查询。因此,即使您认为将 @search 设置为 Searchlogic 对象,但事实并非如此。您将其设置为 WillPaginate 数组,该数组没有您可以调用的方法 .all 。
I was getting confused by this as well. You want to do the following:
In your view just call @contacts.total_entries to get the total count (will_paginate automatically adds that in).
As soon as you call .paginate it triggers the query. So when you even though you think you are setting @search to a Searchlogic object, you aren't. You are setting it to a WillPaginate array which has no method .all that you can call.