rails 4 will_paginate set count of links on pages - ruby-on-rails

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 %>

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.

Sunspot Solr fulltext search and will_paginate

The following query is working like a charm:
#styles = Style.search { fulltext params[:q] }
The problem I'm having is with pagination. Here is the same query with pagination:
#styles = Style.search { fulltext params[:q]; paginate :page => params[:page], :per_page => params[:page_limit] }
I have 11 Style records.
If I have :page => 1 and :per_page => 10 when I search for the 11th record, I get an empty array returned for #styles.results
If I set :page=>2 and do the same search I get the 11th style record.
[11] pry(#<StylesController>)> params[:page]=2
=> 2
[12] pry(#<StylesController>)> x=Style.search {fulltext params[:q]; paginate :page => params[:page], :per_page => params[:page_limit] }
=> <Sunspot::Search:{:fq=>["type:Style"], :q=>"hel", :fl=>"* score", :qf=>"name_textp full_name_textp", :defType=>"dismax", :start=>10, :rows=>10}>
[13] pry(#<StylesController>)> x.results
=> [#<Style id: 15...>]
I thought the point was to paginate the search results, not the actual records in their entirety
What's going on here, and how do I fix it?
EDIT
Ok, let me try explaining this another way. Let's say I have these six records:
1 => 'a'
2 => 'b'
3 => 'c'
4 => 'd'
5 => 'e'
6 => 'f'
Let's say I try to search for 'f'
Letter.search { fulltext 'f'; paginate :page => 1, :per_page => 5 }
My result will be an empty array []
Now let's say I try
Letter.search { fulltext 'f'; paginate :page => 1, :per_page => 6 }
Now my result is [6 => 'f']
my thoughts:
get the results from solr then search your model by ids that solr returned and paginate them, something like this:
#search = Sunspot.search(Snippet) do
fulltext params[:search]
end
#styles = Style.where(id: #search.results.map(&:id)).page(params[:page]).per(5)
what I understood from docs: I didn't tried it
By default, Sunspot requests the first 30 results from Solr. That means you can have 100 of records that might match the searching criteria but you'll see only the first 30, to see the others you have to add the paginate to your solr search, like:
search = Style.search do
fulltext "my cool style"
paginate :page => 2
end
in this case, you should be able to access 2 page. To update the number of sunspot requests you need to write:
search = Style.search do
fulltext "my cool style"
paginate :page => 1, :per_page => 50
end
it should give you 50 results in one page, or paginate :page => 2, :per_page => 50 and results should be divided in 2 pages with max 50 results each.
Try supply a fallback to per_page like so:
#search = Style.search do
fulltext params[:q]
paginate(page: params[:page], per_page: (params[:per] || 15))
end
I also faced the same problem. So I removed :per_page tag in my search
And changed my query as
#styles = Style.search { fulltext params[:q]; paginate :page => params[:page]}
To set the per_page_limit, I used the following code in my initializers
Sunspot.config.pagination.default_per_page = 20
You can use the "hits" method.
In the controller
#search = User.search do
with :name, 'joe'
end
In the view
<% #search.results.each do |result| %>
<%= result.name %>
<% end %>
<%= will_paginate #search.hits unless #search.nil? %>

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%>

Paginating a solr response using kaminari?

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!

Can't sort table in rails

I'm having trouble sorting a single-column table in Rails. Each row represents a single object (an article) and contains all of its attributes (name, content, created_at, user, etc.). The search function works fine (Article.where) but I can't seem to sort the table by any attributes, i.e. Article.order('attribute'). The default, which I can't change, is created_at desc. Am I overlooking something?
Here is my controller:
def index
#title="Home"
if params[:search]
#search=params[:search]
#articles=Article.where('name LIKE ? OR category LIKE ?', "%#{params[:search]}%", "%#{params[:search]}%").paginate(:per_page => 15, :page => params[:page])
else
#articles=Article.order('name').paginate(:per_page => 15, :page => params[:page])
end
end
And view:
<table>
<%= render #articles%>
</table>
<%= will_paginate #articles, :previous_label => "Prev", :next_label => "Next" %>
Use reorder to override any default ordering.
Article.reorder('name').paginate(:per_page => 15, :page => params[:page])
I recommend my gem simple-search for these problems. It may be too simple, but worth a shot.

Resources