问题
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')
回答1:
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 %>
来源:https://stackoverflow.com/questions/10867977/how-to-use-pagination-in-rails-3-2-3