SUPPORT THE SITE WITH A CLICK

Subscribe Rss:

SUPPORT THE SITE WITH A CLICK

Saturday, November 21, 2009

Will paginate page number problem

Mostly for pagination in rails, we will going towards will paginate.The problem we use to face in will paginate is that if anyone hardcoded the url and passes page value to be zero or less than 1.Then its starts throwing the error

"0" given as value, which translates to '0' as page number




Generally, we will be redirecting to 404 error page.But the possible things we can do in our controllers part are
1.By checking the params[:page] we can raise Argument Error, "'page' setting cannot be less than 1
2.params[:page]=params[:page].to_i <=0 ? 1 : params[:page]

When any passes the value of the page to less than zero,it will displays the result with page value 1.

1 comment :

  1. Thanks, I used this to fix this problem, only I revised the code to:

    params[:page] = 1 if params[:page].to_i <= 0

    No biggie, just a tad more readable.

    ReplyDelete