What's a better way to use helper methods with will_paginate? - ruby-on-rails

I'm using the latest version of will_paginate with rails 3. I'd like to use out_of_bounds? to set the current page to the last page if the page parameter is higher than the last page. I found a way to do it like this:
people = People.all.paginate :page => params[:page], :per_page => 20
#people = people.paginate(:page => (people.out_of_bounds? ? people.total_pages : params[:page))
The problem with this is that I have to call paginate twice. The first time to create a WillPaginate::Collection in order to use the out_of_bounds? and total_pages methods, and the second time to actually set the current page. I also need to do this with more actions so it's getting kinda out of hand. I can't use a before_filter or an after_filter, either (I don't think?). Is there a better way to achieve this?

I don't know the best solution, but at least you can do the check before the second call to the method (I suppose that in most of the cases, the page parameter will be ok, so you wont have to call paginate twice):
people = People.all.paginate :page => params[:page], :per_page => 20
#people = people.out_of_bounds? ? people.paginate(:page => people.total_pages) : people

Related

Limit the Elasticsearch results count in Rails

I'm running Rails with Elasticsearch using the elasticsearch-rails and will_paginate gems and things are working great.
The only problem I'm having is that my records count has grown over 10.000 which gives the error "Result window is too large, from + size must be less than or equal to: [10000] but was [10200]" when someone clicks on the last page of the pagination links.
Now I don't want to mess with index.max_result_window because my dataset is going be much much larger and I really don't need visitors to be able to view every page and record anyway.
So basically I just want to set a limit of 10.000 on my results but I'm having no luck getting that working.
This is the query I have in my controller action:
#response = Price.search(params)
#response = #response.paginate(page: params[:page], per_page: 24)
#prices = #response.records.includes(shop: :pictures)
#prices_count = #response.total_entries
#prices = #prices.decorate
#results = #response.results
And I feed the #response to will_paginate using
will_paginate(#response)
I've tried using .limit(10000) in various places and giving a total_entries value to will_paginate but nothing seems to work.
Do you guys have any ideas?
Thanks!
I had the same Issue, in my case I'm using kaminari but should work the same with will_paginate:
= paginate #keyword_search_results
With 25 results per page after 400 page it throws the index.max_result_window error, so I just add the :total_pages option to paginate
= paginate #keyword_search_results, {:total_pages => 400}
Would be a good idea to calculate that value dynamically based on your needs.
use per_page option
#response = Price.search(params, per_page: 24)

Tire + Will_Paginate not taking Tire.options

I'm trying to integrate Tire into my site and I'm having difficulty with pagination. I've tried paginating the results outside of the context of Tire and will_paginate is working on that Array. However, when I try will_paginate within the context of Tire I'm having one large problem.
Will_Paginate will display the correct number of pages with consideration of :per_page but when I click on that page the results are not loaded, rather they are the same as on the first page. The page number is highlighted in the will_paginate navigation.
#results.inspect yields this:
#<Tire::Search::Search:0x007f88ab9153d0 #indices=["deja-set-development"], #types=[], #options={:load=>true, :page=>1, :per_page=>2}, #path="/deja-set-development/_search", #query=#<Tire::Search::Query:0x007f88ab915088 #value={:query_string=>{:query=>"oh"}}>, #facets={"type"=>{:terms=>{:field=>:_type, :size=>10, :all_terms=>false}}}>
Here is where I call will_paginate:
= will_paginate #search_results.results, params
Here is where I iterate through the results
#search_results.results.each
Does anyone have any thoughts?
Edit ---
I'm not sure what is going on, but I did this and it is working.
#search_results = #search_results.paginate(:page => params[:page], :per_page => 5)
Please see the integration test in Tire, and make sure you're passing all options properly.
So to clarify, I've attached my github correspondence with #karmi here.
https://github.com/karmi/tire/issues/627#issuecomment-13449368
I was using Tire.search as opposed to searching by model. As #karmi notes, at the moment :per_page and :page are not supported with Tire.
Here is how I solved this:
#search_results = Tire.search [:index1, :index2, :index3], :load => true, :from => from, :size => size do
query do
string q, :default_operator => 'AND', :fields => [:name1, :name2]
end
end
I ended up having to spin my own small pagination system to increment 'size' and 'from'. Here's the elasticsearch link on the topic.
http://www.elasticsearch.org/guide/reference/api/search/from-size.html
You're able to still access
= #search_results.results.total_entries/next_page/previous_page
which helps with pagination.
Thank you again #karmi.

Limiting Number of Posts Per Page - Ruby on Rails Blog

I am currently developing a Ruby on Rails blog. I have my blog posts show up on the main page, however, I would like to list the posts 5 at a time, so that my frontpage doesn't go on forever and my blog will look much cleaner.
Let me know if you can help. Much appreciated.
Looks like you need a pagination solution - consider using kaminari or will_paginate ( https://github.com/amatsuda/kaminari, https://github.com/mislav/will_paginate/wiki )
And if you need an endless page, there is a nice screencast about that: http://railscasts.com/episodes/114-endless-page
For example, if using will_paginate for pagination, you just call paginate method at end of line your query inside controller, for example inside your controller
def index
#blogs = Blog.all.paginate(:page => params[:page], :per_page => 5)
end
from your view, just simply put:
will_paginate #blogs
at specify location, to show pagination.
If I understand right, you want to limit the number of post on the home page . Then you should do like
Model.find(:all, :limit => 5, :order=> 'created_at desc')
you can remove the order if you don't need it. If you need to make pagination take a look at will_paginate

How do you use will_paginate?

Say if I have a set of objects contained in #set. Each of these objects has a description method which will return some text that I want to display on individual pages. How do I use will_paginate to paginate this?
The examples I've seen so far such as:
#articles = Article.paginate :page => params[:page]
look like they are referring to all Article objects.
Assuming your model is called SomeObject, the will_paginate syntax is similar to that of ActiveRecord's .find():
#set = SomeObject.paginate(:page => params[:page],
:per_page => 20,
:order => 'created_at DESC',
:conditions => { :foo => 'bar' })
Check the documentation for more.
To do do the call in active record you just use .paginate instead of .find and pass the necessary arguments, the gem is pretty well documented on how to use it. Installation docs, and usage examples are in the Readme. The examples might not be using your #set but you use it the same way.
Edit: Apparently those links were wrong, The github repo for will_paginate has that wiki and readme file. Do a search on github for it as I seem to be having epic link fail at the moment.

Pagination and table sorting in Rails

I am using will_paginate and sort helper for pagination and sorting resp..
But im facing one problem while sorting that when im on page 2 or 3 or any other page than first page.
It redirects to the first page and sorts the first page only.
please help me how to sort all the records and go back to page where I was.
What's the reason you need to go back to the page (2 or 3)? Records will change position and probabily page so you will not find them at the same place.
so why you don't change only the :order value (ex. from 'firstname DESC' to 'lastname DESC) when you call the action again?
# people_controller.rb
def index
#people = People.search(params[:my_order], params[:page])
end
# models/people.rb
def self.search(my_order, page)
paginate :per_page => 10, :page => page,
:conditions => ['job like ?', "Driver"],
:order => "%#{my_order}%"
end
I have more powerful solution for this: https://github.com/mynameisrufus/sorted

Resources