I'm new in RoR, and I want to include pagination in my application.
Which is the best way to perform this task? Please suggest me. Is it possible to use will_paginate gem in rails 3.2.3 ?
If yes, in which method I should include:
Post.where(:published => true).paginate(:page => params[:page]).order('id DESC')
Post.page(params[:page]).order('created_at DESC')
Yes, it is possible to use will_paginate gem in rails 3.2.3 applications.
And the code will depends on what each action will perform.
The code below usually goes on a #index method, in your PostsController.
#posts = Post.where(:published => true).paginate(:page => params[:page]).order('id DESC')
So, in your posts/index.html.erb view file you can use the following code to display the pagination links:
<%= will_paginate #posts %>
Related
I'm getting a no method error when trying to implement pagination on my rails project.
undefined method 'paginate' for #<Mongoid::Criteria:0x007fb256e54588>
In my controller I have:
#user = User.all.paginate(:page => params[:page], :per_page => 10)
In my index.html.erb file I have <%= will_paginate #user %> above my html <table> tag and below the closing html </table> tag
I have the will_paginate and mongoid-pagination gems installed properly. And I have tried restarting the dev server.. Am I missing anything else?
You want to use the will_paginate_mongoid gem:
It just creates a paginate method compatible with will_paginate interface [...]
The mongoid-pagination gem requires you to manually include Mongoid::Pagination in your models and doesn't specifically integrate with the will_paginate gem.
The will_paginate gem has support for Mongoid out of the box.
require 'will_paginate/mongoid'
In your controller action
User.all.paginate(page: params[:page], per_page: 10)
I have a Rails application which uses AngularJS for displaying and asynchronously updating a list of objects (configured as AngularJS resources). Is there a simple way to server-side paginate this table?
The will_paginate gem offers a way to paginate queries in Rails controllers and models. From the github README:
## perform a paginated query:
#posts = Post.paginate(:page => params[:page])
# or, use an explicit "per page" limit:
Post.paginate(:page => params[:page], :per_page => 30)
## render page links in the view:
<%= will_paginate #posts %>
You can customize the default page contents:
# for the Post model
class Post
self.per_page = 10
end
# set per_page globally
WillPaginate.per_page = 10
And with Active Record 3 you can do things like:
# paginate in Active Record now returns a Relation
Post.where(:published => true).paginate(:page => params[:page]).order('id DESC')
# the new, shorter page() method
Post.page(params[:page]).order('created_at DESC')
Try doing pagination server side with #ngTasty
git : https://github.com/zizzamia/ng-tasty
docs : http://zizzamia.com/ng-tasty/directive/table-server-side
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.
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
I have a rails app and I'm trying to set up pagination for a view of the Essays class. I'm new to rails... so I can do this for ALL of them, but I only want certain ones to be in this list (where all the essays are contained in Essay.find(Ranking.where(:user_id=>current_user.id).essay_id)).
home.html.erb contains (among other things):
`<%= will_paginate #essays%>
<ul class="users">
<%= render #essays %>
</ul>
<%= will_paginate #essays%>`
in the Pages Controller:
def home
#...
#essays = Essay.paginate(:page => params[:page])
end
I tried adding #essays=Essay.find(Ranking.where(:user_id=>current_user.id).essay_id) before the #essays=Essay.paginate(:page => params[:page]) but the method essay_id for the Ranking class is not available here. How do I get around this? Thanks!
This should work:
Essay.joins(:rankings)
.where(:rankings => {:user_id => current_user.id})
.paginate(:page => params[:page])
While this can be done with will_paginate. I've had some issues with this plugin for Rails 3. A much smoother solution, in my opinion, was to use another pagination plugin called Kaminari.
Assuming, essay_id is passed as a param, you could try:
#ranking = Ranking.where(:user_id => current_user.id, :essay_id => params[:essay_id]).page(params[:page]).per(10)
Or depending on your logic. If the essay object has already been identified in your controller:
#essay = Essay.find(1234)
#ranking = Ranking.where(:user_id => current_user.id, :essay_id => #essay.id).page(params[:page]).per(10)
And then, in your view:
<%= paginate #ranking %>
Even better, to get you started on Kaminari, you can view this rails cast. As noted from the rails cast:
The first-choice gem for pagination in
Rails is will_paginate, but the
currently released version doesn’t
support Rails 3. There is a
pre-release version available that
works but it hasn’t been updated for
several months. If will_paginate is no
longer in active development are there
any other gems we could use?
One alternative is Kaminari. This
seems to provide a cleaner
implementation of pagination and
offers several improved features, too,
so let’s try it in our application
instead.
Hope that helps!
Simply chain paginate method after find method:
#essays=Essay.find(Ranking.where(:user_id=>current_user.id).essay_id).paginate(:page => params[:page])