Reputation: 6493
I am new to rails so go easy. I have built my first blog and would like to set it up such that in the <% post.each do |post| %> render of the posts, I would like it to work such that it only displays 10 posts and then has a link to view the next 10.
I am hoping this is an easy question. Let me know if you would like me to post any code.
Upvotes: 5
Views: 29768
Reputation: 605
I've got problems using "will_paginate", installing the GEM then unistalling... a REAL PROBLEM! So I decide to program the pagination on RoR, it was not difficult so I wanted to share what I did:
Controller:
def index
#how many register per page i want to show
@b = 30
#params via GET from URL
a = params[:page].to_i
#first register per page
a1 = params[:page].to_i * @b
#the query can be easy...
@callcenters = Income.all(:joins => "LEFT JOIN radcheck ON radcheck.id = incomes.radcheck_id", :order => "created_at DESC",:limit => "#{a1},#{@b}", :select => "radcheck.username as clave,caller_id, created_at, value")
#I need to know how many pages somehow
@registrostotales = Income.all(:joins => "LEFT JOIN radcheck ON radcheck.id = incomes.radcheck_id", :select => "radcheck.username as clave,caller_id, created_at, value").count
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @callcenters }
end
end
View:
...
Total de registros: <%= numero = @registrostotales %><br/><br/>
<!-- total pages -->
<% pages = @registrostotales / @b %>
<!-- the last page -->
<% if pages % @b > 0 %><% pages = pages + 1 %><% end %>
Paginas:
<% (0..(pages-1)).each do |i| %>
<!-- href or link_to, http://xxxxxxxx/yyyy?page=i -->
<%= link_to i+1, :action => "index", :controller => "callcenters", :page => i %>
<% end %>
<!-- the view.. -->
<table>
<tr>
<th>#</th>
<th>Teléfono (ID)</th>
<th>Zona</th>
</tr>
<% @callcenters.each do |callcenter| %>
<tr>
<td><%= numero - params[:page].to_i * 30 %><% numero = numero - 1 %></td>
<td><%= callcenter.caller_id %></td>
<td><%= Node.first(:conditions => {:calling_id => callcenter.caller_id}).description %></td>
</tr>
<% end %>
</table>
I hope this helps to someone!
Upvotes: 5
Reputation: 9376
You should use the gem will_paginate.
The installation and usage is really easy: https://github.com/mislav/will_paginate/wiki/installation
Example usage: http://github.com/mislav/will_paginate
Upvotes: 8
Reputation: 535
Adding pagination to a Ruby on Rails app
To add pagination to your Ruby on Rails app, you have to modify the following files:
Gemfile:
gem 'will_paginate', '~> 3.1.0'
gem 'will_paginate-bootstrap'
Areas controller --> index
@areas = Area.pagination_request(params[:page])
index.html.erb
<%= will_paginate @areas, renderer: BootstrapPagination::Rails %>
Model file:
def self.pagination_request(page)
paginate :per_page => 10, :page => page
end
Upvotes: 0
Reputation: 125
You can use the kaminari gem.
Very ease to use and highly customization friendly.
Upvotes: -1
Reputation: 1484
We can do this with ease by using 'will_paginate-bootstrap' gem.
To continue firstly you add a line to the gem file as,
gem 'will_paginate-bootstrap'
.
Now run bundle install.
In the controller, you will be using like @post = Post.all
to get the
contents from models.
Instead use this
@post = Post.paginate(:page=>params[:page],per_page:5)
Here in the above line per_page field is to specify how many records you need in a page.
<%= will_paginate @post,renderer: BootstrapPagination::Rails %>
Note: I thought Post as modal and post as variable declared in the controller. Just go with your variables.
Upvotes: 0
Reputation: 13765
will_paginate is definitely the way to go, I just thought I'd add a link to a railscasts will_paginate video showing you how to use it since sometimes that can be an easier way to learn the basics than reading documentation. Plus you'll learn a brief history on how pagination used to be done when it was an included part of Rails (it was slower and more cumbersome). The old classic pagination has been moved out into it's own plugin too, but it's only recommended if you were already using it when you upgraded Rails to a version where they took it out.
Railscasts has a ton of other great videos that you might find helpful. For example, once you get more advanced you might want to try ajax pagination
Upvotes: 7
Reputation: 176362
Check out the will_paginate Gem.
It’s very easy to do pagination on ActiveRecord models:
@posts = Post.paginate :page => params[:page], :order => 'created_at DESC'
In the view, page links can be rendered with a single view helper:
<%= will_paginate @posts %>
Upvotes: 5