Paginating a solr response using kaminari? - ruby-on-rails

Does kaminari support pagination of a solr response ? If yes, how to convert the response to a kaminari-compatible format ?

Set up Kaminari for your project as described here - http://railscasts.com/episodes/254-pagination-with-kaminari
In your controller, if you have the following to perform the Solr search -
#vehicles = Vehicles.search do
with (:year => 2012)
paginate :page => params[:page], :per_page => 20
end
You need to add the following in your view to get the pagination links -
<%= paginate #vehicles.hits %>
Hope that helps!

Related

Ruby on Rails: Show default result before using dropdown filter with will_paginate

I can display the results by 10, 15, or 20 by using a drop-down. The issue is when the app loads it displays all results. I would like to display only 10 results by default before using the drop-down filter to display more results.
Can someone help please?
Thank you!
This is my VIEW:
<%= select_tag :per_page, options_for_select([10,15,20], #per_page), :onchange => "if(this.value){window.location='?per_page='+this.value;}" %>
This is my CONTROLLER:
#per_page = params[:per_page] || Post.per_page
#posts= Post.all.paginate(:per_page => #per_page, :page => params[:page])
Try this code:
#per_page = params[:per_page] || Post.per_page
#posts= Post.paginate(per_page: #per_page, page: params[:page])
Note that I call paginate without on Post class itself and not on result of Post.all.
I also removed the last || 10 part. I think you don't need it there as you already set this value on Post model.

rails 4 will_paginate set count of links on pages

I'm using rails 4 and gem will_paginate. I can't customize count of links on pages before gap (...). eg now I having 9 pages than ellipsis and 3 last pages. How can I set 5 pages instead 9 (see scrennshots)?
In controller:
#per_page = params[:per_page] || 6
#events = Event.not_cancelled.paginate(:page => params[:page], :per_page => #per_page)
Now
That I want
You must change value for inner_window. Reference here.
So here is code:
<%= will_paginate #events, :inner_window => 2, :outer_window => 1 %>

How do pagination on hash that have value in form of array using Rails 4

I have hash that have values in form of array. i am using will paginate for pagination. But recieve error
"undefined method paginate for Hash".
below is my code
h = {"127.1.0"=>[31.553965003401203, 74.35798642106394], "127.2.0"=>[54.99947245294339, 74.35810745776595], "127.0.0"=>[51.07178714109552, 71.86443937665145]}
h.paginate(:page => params[:page], :per_page => 30) #giving error
Any help to done this...
It looks like you can use the will_paginate/array module which would at least get you paginating on an array. Try something like this.
require 'will_paginate/array'
h = {"127.1.0"=>[31.553965003401203, 74.35798642106394], "127.2.0"=>[54.99947245294339, 74.35810745776595], "127.0.0"=>[51.07178714109552, 71.86443937665145] }
h.keys.paginate(page: params[:page], per_page: 30)
(similar to Ruby on Rails will_paginate an array)

Getting total result count from Rails query using will_paginate

I get a list of objects from my Rails app, and use will_paginate to page as usual, and then I have a little method used to save details of the search to the database:
per_page=10
session[:search_params] = params[:search_people]
#documents = Person.search_people(params[:search_people], params[:page], per_page)
Search.create(:user_id => (!current_user ? 0 : current_user.id),
:search_type => "Person",
:firstname => params[:search_people][:first_name],
:lastname => params[:search_people][:last_name],
:results => #documents.count )
The problem is, the number of search results (#douments.count) is always <= per_page used for will_paginate.
I understand why this is, but is there a way around it without running the query twice, once with will_paginate and once without?
Try <%=#documents.total_entries%>

Ruby on Rails: Default will_paginate to last page of results

Is there an easy way, to default will_paginate to last page? I would like to show user latest addition and allow to browse previuos pages of results...
Just order your query so that it's in reverse chronological order.
Post.paginate(:page => (params[:page] || 1), :per_page => 20 :order => "created_at desc")
The proper way to do it is to reverse the sorting order, i.e. add
:order => 'created_at DESC'
to your paginate call. User would expect the "latest addition" on the beginning, and older ones on the following pages.
This solution was the most resonable idea I found:
http://groups.google.com/group/will_paginate/browse_thread/thread/63b8d295f25085c2

Resources