Item's page and will_paginate
page = (number_of_records_before_RECORD / number_of_records_per_page) + 1
In other words. If your photo has ID 40 and there are 25 records before (assuming some records have been deleted), with 20 records per page:
page = (25 / 20) + 1 = 2
You can count the number of records before the selected record using Model.count(:conditions => ['id < ?', record.id], :order => 'id')
.
The right query depends on which sorting filter you apply to that table when listing all objects.
will_paginate on array only displays first page of items
I think you missing page: params[:page] in index controller, this is how will_paginate know what page you currently now
def index
@items = Array.new
API.items.all! do |i|
@items << i
end
@paginated_items = @items.paginate(page:params[:page],per_page:25)
end
Correct number for each item in will_paginate list?
A will_paginate
Collection
is not just a simple Array
or ActiveRecord::Relation
. Instead it has some additional methods defined, for example: current_page
, per_page
, offset
, total_entries
or total_pages
.
You can use Collection#offset
to calculate your current index:
<% @collection.each_with_index do |item, index| %>
<%= @collection.offset + index + 1 %>
...
<% end %>
What can be simplified by initializing Enumerator#with_index
with the first index (note the .
between each
and with_index
:
<% @collection.each.with_index(@collection.offset + 1) do |item, index| %>
<%= index %>
...
<% end %>
show items per page with will paginate
Okay, so basically: XHR request -> controller -> action -> rendered view as return value.
You need to call your controller. This is what you do already.
Second, the data retrieved by the controller should be used to update the current page. For this, I would extend your respond_to with:
respond_to do |format|
format.js
end
Means that JS requests will render an index.js.{erb,rjs,...} handler. In this, you write your code to update the view. The rendered output is what you are going to receive as return value in your javascript.
Last, you need to evaluate the response.
Hint: You are calling your index action via javascript. You should do this with GET and not with POST.
will_paginate is there a way to set per_page to 'all'
Item.paginate_by_sql(sql, :page => params[:page], :per_page => Item.count)
Getting the current item number or index when using will_paginate in rails app
It feels a bit hacky, but I've come up with a working solution. I create an instance variable in the view:
@count = @movies.total_entries - @movies.offset
Then when rendering each movie I output @count and decrement it.
As per my previous example with 51 items on two pages: total_entries == 51, and offset == 0 (page 1) or 50 (page 2).
Rails 3 and will_paginate - elegant way, how to get total_pages
if you have a posts model,
<%= @posts.total_pages %>
will give you the pages total, i.e you need to reference the model you're calling will_paginate on
http://rdoc.info/github/mislav/will_paginate/master/WillPaginate/Collection
Related Topics
How to Search for Useful Ruby Gems
Ruby on Rails - Activerecord::Relation Count Method Is Wrong
Rails 5 - Using Polymorphic Associations - Rendering the Views
Why Can't I Create an Array as a Column in a Table in Rails
Test (With Rspec) a Controller Outside of a Rails Environment
Error Building Ruby in Sublime Text Editor
How to Change Ruby to Version 1.9.3 (Again) with Rvm
Rails Error Message Displays Key and I Only Want Value
Ruby: Cannot Install Watir Gem on Windows
Ruby Private Attr_Accessor and Unexpected Nil
Triple Single Quote VS Triple Double Quote in Ruby
Why Is My Ruby Git Script Hook Run with the Wrong $Path
Ruby, Generate a Random Hex Color (Only Light Colors)
How to Get Gmail Oauth or Xauth Tokens with Omniauth
Rotating Letters in a String So That Each Letter Is Shifted to Another Letter by N Places
Problem Running Thinking Sphinx with Rails 2.3.5