I am fetching some rows from the database in my controller with this query :
@main = connection.execute("select code_ver, result from mastertest;")
Now in my html.erb, I am displaying the results in a table like this:
<% @level1.each do |row1| %>
<table id="tbl_main1" name="tbl_main1">
<tr bgcolor="#D1D1D1">
<td width="60%" <%= row1[0] %>></td>
<td width="20%"><%= row1[2] %></td>
<td width="20%"><%= row1[1] %></td>
</tr>
</table>
I am fetching a lot of rows here. But I want to show only say 15 results at once, and have some kind of buttons 'next' and 'previous' which would cycle through the results. How would I do that ?
If you're using Rails 3, have a look at kaminari for pagination: https://github.com/amatsuda/kaminari. You are probably interested in the Paginating a generic Array object feature. However, keep in mind that this won't add offsets and limits to your SQL query.
Kaminari is widely considered better than will_paginate for Rails 3, since it takes advantage of Rails scopes and doesn't globally pollute objects.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With