will_paginate works on local box, gives "undefined method to_i" on server but both point to the same database
We just ran into this problem the other day all of a sudden. Our Rails app is using will_paginate, and I have it defined as follows in my controller:
# contacts_controller.rb
def index
# ...
@search = @current_user.contacts.search(params[:search])
@contacts = @search.all.paginate({:page => params[:page], :per_page => 20})
end
Both development and staging (this is a staging box not production) point to the same database. The above code works fine on my local machine, however on staging I get the following error:
undefined method 'to_i' for {:per_page=>20, :page=>nil}:Hash
The code is identical on both computers. Any idea why it works fine on one and not on the other?
EDIT: On the staging server I was using the plugin while on my local box I was using the gem, however I removed the plugin and installed the gem and now I get an error that says:
uninitialized constant Array::WillPaginate
Evidently it is still not solved.. after doing a deploy on the staging server I'm getting that error again, despite the fact that it was working before fine. Anyone have any ideas at all on this? I'm totally clu开发者_如何学Goeless.
If the page param is nil, it's often a good idea to default to a value of 1. This covers cases where no url parameter for page is passed. Where a parameter is passed, it will be used in place of the default:
{:page => params[:page] || 1, :per_page => 20})
Full example:
# contacts_controller.rb
def index
# ...
@search = @current_user.contacts.search(params[:search])
@contacts = @search.all.paginate({:page => params[:page] || 1, :per_page => 20})
end
Well, I seem to have solved it:
I had to explicitly type require 'will_paginate'
in the environment after I uninstalled the plugin and installed the gem. However I'm still not sure why the plugin was working on my local machine but not on staging.
精彩评论