will_paginate find out if I'm on the last page
@collection.total_pages == @collection.current_page
will_paginate show the last page by default (keeping chronological order)
This is the solution I finally arrived to:
if params[:page]
@collection = @q.result(distinct: true).paginate(:page => params[:page], :per_page => 30)
else
last_page = @q.result(distinct: true).paginate(:page => params[:page], :per_page => 30).total_pages
@collection = @q.result(distinct: true).paginate(:page => last_page, :per_page => 30)
end
It's not fancy, it needs some refactoring and perhaps can use memoization to avoid calling the paginate
method twice, but hey it works. If you are trying to access a page directly, it is rendered as usual, but if you don't specify a page, you are taken to the last page by default. Displaying message on last page with will_paginate gem
One of the way to implement this is adding some conditions on view:
<% prev = (@posts.total_pages == @posts.current_page) ? '' : 'prev' %>
<%= will_paginate @posts, :page_links => false, :next_label => 'more', :previous_label => prev %>
(will_paginate) Find the page a record is in
HOWTO rank items by balance in Ruby on rails
How to fetch the first post when the last in reached in will_paginate and Rails 3?
WillPaginate actually lets you define the collection it uses directly with WillPaginate::Collection.create
. I think a bit of code like the following should do the trick:
@posts = Post.offset((page - 1) * per_page).limit(per_page)
post_count = @posts.count
if post_count < per_page
@posts = @posts.all + Post.limit(per_page - post_count).all
end
# At this point you have an array of posts.
# Now we create the WillPaginate::Collection so will_paginate will work.
@posts = WillPaginate::Collection.create(page, per_page) do |pager|
pager.replace(@posts)
pager.total_entries = Post.count
pager
end
will_paginate not working on records retrieved with find
Perhaps this:
Bookmark.paginate(:conditions => {:id => [21,23]}, :page => 1)
Or Simpler:Bookmark.where(:id => [21,31]).page(1)
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
Related Topics
Best Way to Create a Blog with Static Pages in Ruby
Heroku Repo Size and Slug Size Increase with Each Deployment. Why
Rails Redirecting Invalid Route to Root
How to Open File in Default Application. Ruby
How to Tell Unicorn to Understand Heroku's Signals
Why Does The Ruby Module Kernel Exist
How to Parse Days/Hours/Minutes/Seconds in Ruby
Fixing The "Ruby Installation Is Missing Psych" Error
Need Help Installing Ruby 2.7.2 on Mac
How to Use (Ruby) Rgeo to Transform (Unproject) Coordinates
How to Properly Manage The Rails Tmp Directory
Sinatra on Nginx Configuration - What's Wrong
What's The Best Way to Test Delayed_Job Chains with Rspec
How to Reference a Local Gem from a Ruby Script
Deleting Items from an Array Requires Multiple Passes to Remove Them All
Dbi::Interfaceerror: Could Not Load Driver (Uninitialized Constant MySQL error)