Hey,
I have a forum and at the bottom of each page of the current topic I
have a form to add a reply.
I am using the following pagination to display 20 posts per topic per
page...
page = (params[:page] ||= 1).to_i
posts_per_page = 20
offset = (page - 1) * posts_per_page
@posts = Post.find(:all, :conditions => ["topic_id = ?",
params[:id]], :order => "created_on DESC")
@post_pages = Paginator.new(self, @posts.length, posts_per_page,
page)
@posts = @posts[offset..(offset + posts_per_page - 1)]
My question is:
When a user adds a reply, I want to then redirect that person to the
last page of posts (which is where their reply will have been added)
Any ideas how i could go about this?
Thanks!!
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
Scott Holland wrote:> page = (params[:page] ||= 1).to_i > posts_per_page = 20 > offset = (page - 1) * posts_per_page > @posts = Post.find(:all, :conditions => ["topic_id = ?", > params[:id]], :order => "created_on DESC") > @post_pages = Paginator.new(self, @posts.length, posts_per_page, > page) > @posts = @posts[offset..(offset + posts_per_page - 1)] > > When a user adds a reply, I want to then redirect that person to the > last page of posts (which is where their reply will have been added) > Any ideas how i could go about this?Pagination is a highly debated topic with several ways of implementing it. While it may be better to use one of the pagination plugins, here is a suggestion based on your current implementation. To get the number of total posts, use: Posts.count - your current code selects every single post from the database on a request to that action, which is inefficient and will not scale. You could support a custom ?page=last parameter. total = Post.count limit = 20 page = params[:page] || 1 page = (total.to_f / limit).ceil if page == "last" @posts = Topic.find(params[:id]).posts.find(all, :limit => limit, :offset => (page.to_i-1)*limit, :order => "created_on DESC") @post_pages = Paginator.new(self, total, limit, page.to_i) I''m not sure this is the cleanest way to implement what you want, but it will work. redirect_to :controller => ''posts'', :action => ''list'', :page => ''last'' Dan Manges --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Hey, Thanks for that. I''ll give it a go. I never actually thought about pagination plugins. Are there any you would recommended? Scott -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
question about pagination and how it does not scale well, what size recordset will pagination start being a problem? < 1000, > 2000, a few hundred? or are we talking 10k and above? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---