I want to display all standards in my view. But it should display 10 records per page. so I used will_pagination
to handle this case. When I click first page, it displayed first 10 records of standards with S.No . But When I click second page, the S.No of 11th record shows "1". But the S.No should be 11 for the 11th record. It's not working. what will be the issue?
Controller:
@standards = Standard.select("standards.standard_id, standards.standard_name")
.where("standards.org_id = ?", org_id)
@standards = @standards.paginate(:per_page => 10, :page => params[:page])
View:
<% if (@standards != nil && @standards.length > 0) then%>
<% @standards.each.with_index(1) do |standard,index| %>
<tr>
<td> <%= index %></td>
<td> <%= standard.standard_name %></td>
</tr>
<% end %>
<div class="text-right">
<% if @standards != nil then%>
<%= will_paginate @standards, :class => 'pagination-xs', renderer: BootstrapPagination::Rails %>
<%end%>
</div>
Use the param value to get the serial number:
<% count = ((params[:page] || 1).to_i - 1) * 10 %>
<% if (@standards != nil && @standards.length > 0) then%>
<% @standards.each.with_index do |standard,index| %>
<tr>
<td> <%= count + index %></td>
<td> <%= standard.standard_name %></td>
</tr>
<% end %>
<div class="text-right">
<% if @standards != nil then%>
<%= will_paginate @standards, :class => 'pagination-xs', renderer: BootstrapPagination::Rails %>
<%end%>
</div>
UPDATE
In you controller code:
@standards.paginate(:per_page => 10, :page => params[:page])
You are saying that you need 10 records per page and you have set the page number param as params[:page]
Initially we are not getting any params
since it is page 1. So if no param value is received we have to take it as 1.
params[:page] || 1
In that case count = (1-1)*10 = 0
Consider you are on the second page and let your URL be like this:
localhost:3000/standards?page=2
Now you have params[:page] = 2
And count = (2 - 1)*10 = 10
So 10
will be added to the serial number of each record in the second page .