updated the page method, added args to AR page method #276

wants to merge 5 commits into from

4 participants


In my fork its possible to set a default_page as a argument for the page method. If param[:page] is invalid, the page method would return the default_page and is not raising an exception, like it is now for the issue #271

params[:page] = 'foobar'
posts = Post.where(:published => true).page(params[:page])
#posts.current_page = 1

# set default_page if necessary 
posts = Post.where(:published => true).page(params[:page], :default_page => 10)
#posts.current_page = 10
# this raises no exception, it trys to return the default_page

with best regards

Hannes Maack added some commits Nov 5, 2012
Hannes Maack get page number one for invalid page_numbers 4c00624
Hannes Maack reverted page_number.rb to original, added args to page method for ac…
…tive-record, now its possible to add default_page param e.g. page('foo', :default_page => 2) to get no exception in case of invalid pagenum ("foo") and to set a default page if its not page one
Hannes Maack changed read me for docu a4ae230
Hannes Maack fixed read me da1b84c

Any chance to get this fixed? Users keep changing URL and we get lots of errors because of this... cc @mislav


Nope, I don't find the feature useful. The exception is deliberate so you can handle it in your application.

posts = Post.where(:published => true)
  posts = posts.page(params[:page])
rescue ArgumentError
  posts = posts.page(default_page)

Or you could massage params[:page] before you pass it in will_paginate.

@mislav mislav closed this Jan 10, 2013

In every action in your application that uses pagination? That's not very practical.


@mislav I think I could handle that exception in our apps if it would be different from the too common ArgumentError. What about raising a custom exception?


@dgilperez You could be handling WillPaginate::InvalidPage instead of ArgumentError.


@mislav hmm that's exactly what I'd rather do, but the exceptions in my controllers look like ArgumentError: invalid value for Integer(): "vdpcaglxx" (sorry, I just realized I should have commented in #271, not here)


@mislav sorry for being too fast on that. Peeping on the code I see InvalidPage as a module that tags that exception ... not used to that pattern, but I it works. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment