[英]Ruby on rails - Kaminari - Paginate array of search results
I have a search form with pagination. 我有一个带有分页的搜索表。 Both things work OK separately, but if I search and then load the next page, the search parameters are forgotten and kaminari shows results regardless of what search options I selected.
两者都可以单独运行,但是如果我搜索然后加载下一页,则无论我选择了哪种搜索选项,搜索参数都将被遗忘,并且kaminari会显示结果。
I have read through the docs, and other stackoverflow posts but I'm not having any luck. 我已经阅读了文档和其他stackoverflow帖子,但是我没有任何运气。
My index page has 我的索引页有
<%= search_form_for @q, html: { id: "gig_search_form" }, remote: true do |f| %>
bla bla bla...
<% end %>
<%= link_to_next_page @gigs, 'Next Page', :remote => true %>
index.js.erb index.js.erb
<% if params[:page] %>
$('.gig-search-list').append("<%= j render(partial: 'gigs') %>");
<% else %>
$('.gig-search-list').html("<%= j render(partial: 'gigs') %>");
<% end %>
gigs partial: 演出部分:
<% @gigs.each do |gig| %>
bla bla bla...
<% end %>
Now, I have tried a few tings in my controller but I'm always gettings the same results. 现在,我在控制器中尝试了一些操作,但总是得到相同的结果。 Originally I had;
原来我有;
def index
if params[:search].present?
@q = Gig.notexpired.where(:filled => false).near(params[:search], 500, :order => 'distance' ).ransack(params[:q])
else
@q = Gig.notexpired.where(:filled => false).ransack(params[:q])
end
@allgigs = @q.result(distinct: true)
@gigs = @q.result(distinct: true).page(params[:page]).per(5)
respond_to do |format|
format.js
format.html
end
end
Then; 然后;
def index
if params[:search].present?
@q = Gig.notexpired.where(:filled => false).near(params[:search], 500, :order => 'distance' ).ransack(params[:q])
else
@q = Gig.notexpired.where(:filled => false).ransack(params[:q])
end
@allgigs = @q.result(distinct: true)
@gigs = Kaminari.paginate_array(@allgigs).page(params[:page]).per(5)
respond_to do |format|
format.js
format.html
end
end
Then I tried to separate them; 然后我试图将它们分开;
def index
if params[:search].present?
@q = Gig.notexpired.where(:filled => false).near(params[:search], 500, :order => 'distance' ).ransack(params[:q])
else
@q = Gig.notexpired.where(:filled => false).ransack(params[:q])
end
@allgigs = @q.result(distinct: true)
if @allgigs.present?
unless @allgigs.kind_of?(Array)
@gigs = @allgigs.page(params[:page]).per(5)
else
@gigs = Kaminari.paginate_array(@allgigs).page(params[:page]).per(5)
end
end
respond_to do |format|
format.js
format.html
end
end
But I get the same result each time. 但是我每次都得到相同的结果。 I can see on the console that when I load more pages, the search parameters are not included.
我在控制台上看到,当我加载更多页面时,不包括搜索参数。
How can I link the two things together and paginate the search results correctly? 如何将这两件事链接在一起并正确对搜索结果进行分页?
UPDATE: 更新:
Server log after loading 'next page' using @chaitanya's answer. 使用@chaitanya的答案加载“下一页”后的服务器日志。 I have previously added search params.
我以前添加了搜索参数。
Started GET "/gigs?locale=en&page=2" for 127.0.0.1 at 2016-06-29 14:45:53 +0200
Processing by GigsController#index as JS
Parameters: {"locale"=>"en", "page"=>"2"}
Gig Load (10.3ms) SELECT DISTINCT "gigs".* FROM "gigs" WHERE (gigzonetime > '2016-06-29 12:45:53.493341') AND "gigs"."filled" = $1 [["filled", "f"]]
Gig Load (1.2ms) SELECT DISTINCT "gigs".* FROM "gigs" WHERE (gigzonetime > '2016-06-29 12:45:53.493341') AND "gigs"."filled" = $1 LIMIT 5 OFFSET 5 [["filled", "f"]]
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 7]]
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 2]]
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 6]]
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 1]]
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 8]]
Rendered gigs/_gigs.html.erb (13.4ms)
(0.5ms) SELECT DISTINCT COUNT(DISTINCT "gigs"."id") FROM "gigs" WHERE (gigzonetime > '2016-06-29 12:45:53.493341') AND "gigs"."filled" = $1 [["filled", "f"]]
Rendered gigs/index.js.erb (17.2ms)
Completed 200 OK in 54ms (Views: 21.4ms | ActiveRecord: 13.0ms)
Server log when filtering with search form: 使用搜索表单过滤时的服务器日志:
Started GET "/gigs?locale=en&utf8=%E2%9C%93&search=&q%5Bdate_gteq%5D=&q%5Bdate_lteq%5D=&q%5Bgenres_id_in%5D%5B%5D=&q%5Bsalary_cents_gteq_euros%5D=0.00&q%5Bsalary_cents_lteq_euros%5D=210.00" for 127.0.0.1 at 2016-06-29 14:45:47 +0200
Processing by GigsController#index as JS
Parameters: {"locale"=>"en", "utf8"=>"✓", "search"=>"", "q"=>{"date_gteq"=>"", "date_lteq"=>"", "genres_id_in"=>[""], "salary_cents_gteq_euros"=>"0.00", "salary_cents_lteq_euros"=>"210.00"}}
Gig Load (0.6ms) SELECT DISTINCT "gigs".* FROM "gigs" WHERE (gigzonetime > '2016-06-29 12:45:47.973892') AND "gigs"."filled" = $1 AND ("gigs"."salary_cents" >= 0 AND "gigs"."salary_cents" <= 21000) [["filled", "f"]]
Gig Load (0.5ms) SELECT DISTINCT "gigs".* FROM "gigs" WHERE (gigzonetime > '2016-06-29 12:45:47.973892') AND "gigs"."filled" = $1 AND ("gigs"."salary_cents" >= 0 AND "gigs"."salary_cents" <= 21000) LIMIT 5 OFFSET 0 [["filled", "f"]]
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 1]]
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 8]]
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 1]]
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 7]]
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 3]]
Rendered gigs/_gigs.html.erb (12.2ms)
(0.4ms) SELECT DISTINCT COUNT(DISTINCT "gigs"."id") FROM "gigs" WHERE (gigzonetime > '2016-06-29 12:45:47.973892') AND "gigs"."filled" = $1 AND ("gigs"."salary_cents" >= 0 AND "gigs"."salary_cents" <= 21000) [["filled", "f"]]
Rendered gigs/index.js.erb (15.6ms)
Completed 200 OK in 33ms (Views: 19.9ms | ActiveRecord: 5.7ms)
尝试按照以下代码将参数带到下一页
= link_to_next_page @gigs, "Next Page", :remote => true, :params => params
You should add the #page
and #per
methods to the query itself. 您应该将
#page
和#per
方法添加到查询本身。 Kaminari.paginate_array
actually takes a slice from the whole array which is not very efficient. Kaminari.paginate_array
实际上从整个数组中获取一个切片,效率不是很高。
Here's a snippet 这是一个片段
def index
if params[:search].present?
@q = Gig.notexpired.where(:filled => false).page(params[:page]).per(5).near(params[:search], 500, :order => 'distance' ).ransack(params[:q]).results
else
@q = Gig.notexpired.where(:filled => false).page(params[:page]).per(5).ransack(params[:q]).results
end
#...
Then use the Kaminari
paginate helper in your view 然后在您的视图中使用
Kaminari
分页助手
<%= paginate @q %>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.