[英]Using searchlogic with will_paginate
EDIT Looks like I figured it out - I had to call paginate after the call to all
from Searchlogic. 编辑好像我想通了-从Searchlogic致电all
后,我不得不打电话给paginate。
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
. 这给我Undefined method 'all' for WillPaginate
的错误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 (eg contact_contact_contact..._path
), presumably because the default "per page" is 20. 删除所有内容会给我一个错误,因为视图正在查找包含单词“ contact” 20次的contact_contact_contact..._path
(例如contact_contact_contact..._path
),大概是因为默认的“每页”为20。
What am I doing wrong? 我究竟做错了什么? I would like to have searching, ordering and pagination all on this page. 我想在此页面上全部进行搜索,订购和分页。
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). 在您的视图中,只需调用@ contacts.total_entries即可获取总数(will_paginate会自动添加总数)。
As soon as you call .paginate it triggers the query. 一旦您调用.paginate,它就会触发查询。 So when you even though you think you are setting @search to a Searchlogic object, you aren't. 因此,即使您认为自己将@search设置为Searchlogic对象,也没有。 You are setting it to a WillPaginate array which has no method .all that you can call. 您正在将其设置为WillPaginate数组,该数组没有可以调用的所有方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.