将 searchlogic 与 will_paginate 结合使用

发布于 2024-08-07 23:10:24 字数 655 浏览 5 评论 0原文

编辑 看起来我已经弄清楚了 - 我必须在从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

未蓝澄海的烟 2024-08-14 23:10:24

我也被这个搞糊涂了。您想要执行以下操作:

class ContactsController < ApplicationController
  def index
    @search = Contact.search(params[:search])
    @contacts = @search.paginate(:page => params[:page])
  end
end

在您的视图中,只需调用 @contacts.total_entries 即可获取总计数(will_paginate 会自动添加该计数)。

一旦你调用 .paginate 它就会触发查询。因此,即使您认为将 @search 设置为 Searchlogic 对象,但事实并非如此。您将其设置为 WillPaginate 数组,该数组没有您可以调用的方法 .all 。

I was getting confused by this as well. You want to do the following:

class ContactsController < ApplicationController
  def index
    @search = Contact.search(params[:search])
    @contacts = @search.paginate(:page => params[:page])
  end
end

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文