Ruby on Rails: Default will_paginate to last page of results - ruby-on-rails

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

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.

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.

reverse page numbers with will_paginate

Is there any simple way to reverse page numeration with will_paginate?
I want for the top page (last time-wise) to be #1, and the last (earliest) to be #N.
The reason for that is page contents shouldn't change with time, which is good for SEO.
Order your query by an ascending date instead of a descending date
def index
#posts = Post.paginate :page => params[:page], :order => "created_at ASC"
end
I'm not sure I understand. If the oldest content is #N, then when there's new content, the oldest content will be pushed to #N+1 and the page contents would change. That sounds like it's the opposite of what you're looking for?
You can pass :order => "column ASC" or :order => "column DESC" to paginate to determine what ends up on page #1.

Resources