问题
I have kinda blog/wiki application where I'd like the home page to include a welcome/landing message and the 5 most recent blog entries and pagination linking to older entries.
Is it possible to have, for example, 5 pages returned as page one of paginated search results and 15 for subsequent pages? I'm currently using will_paginate.
回答1:
You can use WillPaginate::Collection
, here is an example you can use for this :
def self.find_with_pagination(params = {})
WillPaginate::Collection.create(params[:page].to_i < 1 ? 1 : params[:page], per_page_for_page(params[:page])) do |pager|
# inject the result array into the paginated collection:
pager.replace(find(:all, params.merge({:limit => pager.per_page, :offset => pager.offset)}))
unless pager.total_entries
# the pager didn't manage to guess the total count, do it manually
pager.total_entries = self.count
end
end
end
def self.offset_for_page(page_number)
page_number.to_i > 1 ? ((page_number.to_i - 2) * 15 + 5) : 0
end
def self.per_page_for_page(page_number)
page_number.to_i > 1 ? 15 : 5
end
I hope it would help, here is a link for the doc : http://rdoc.info/github/mislav/will_paginate/master/WillPaginate/Collection
回答2:
To me, it sounds like you have two distinct views that you're trying to combine into one: "Welcome" and "Archives". It might be simpler just to split your one page into two:
- The "Welcome" page, which shows the welcome message, the latest X posts, and a link to "Older Posts".
- The "Archives" page, which has all the posts,
will_paginate
d as necessary. Yes, the first five posts will show up here, too, but that's expected (and probably good) in an archive.
Just a different way to think about things - hope it helps!
回答3:
I haven't tried this, but perhaps by overriding the params[:per_page] on the subsequent pages will work. Something like:
Since the controller is something like:
@posts = Post.paginate :page => params[:page], :per_page => 10, :include => [:posts], :conditions => ["post.user_id = ?", current_user.id], :order => "title,created_at"
the view perhaps could have something like this in it:
<%= params[:page] == 1 ? will_paginate @posts : will_paginate @posts, :per_page => 15 %>
回答4:
Yes, the answer is a bit old, but I will give here my solution on Rails 4.2, because it is a bit different.
I need 10 results on first page, and 12 - on others.
item.rb
def self.find_with_pagination(params = {}, filters = {})
WillPaginate::Collection.create(params[:page].to_i < 1 ? 1 : params[:page], per_page_for_page(params[:page])) do |pager|
result = Item.all.limit(pager.per_page).offset(offset_for_page(params[:page])).where(filters)
pager.replace result
unless pager.total_entries
# the pager didn't manage to guess the total count, do it manually
pager.total_entries = self.count
end
end
end
def self.offset_for_page(page_number)
page_number.to_i > 1 ? ((page_number.to_i - 2) * 12 + 10) : 0
end
def self.per_page_for_page(page_number)
page_number.to_i > 1 ? 12 : 10
end
your_super_controller.rb
@items = Item.find_with_pagination(params, @filters)
来源:https://stackoverflow.com/questions/4829604/rails-pagination-different-per-page-value-page-1-from-subsequent-pages