will_paginate undefined method. The Will_paginate

2019-02-27 09:50发布

问题:

I'm using rails 2.3.8

my error:

undefined method `will_paginate' for #<ActionView::Base:0x1124bfa10>

in environment.rb:

require "bundler/setup"
Bundler.require(:default)

in my Gemfile:

gem "will_paginate", "2.3.14"

in the view:

<%= will_paginate(@subscriptions) %>

in the controller:

@search = Subscription.search(params[:search])
@search.order ||= :ascend_by_account_id

@subscriptions = @search.all.paginate(:include => :account, :page => params[:page], :per_page => 30)

Now... will paginate methods appear to work in the rest of the app... it's just that this will_paginate method that is supposed to include pagination links doesn't work. =\

Now, I just started using bundler, and before bundler, everything worked all the time. in my environment.rb I do

require "bundler/setup"
Bundler.require(:default)

right before the Rails::Initializer call

and here is the :default part of my gem file:

source "http://rubygems.org"
# system
gem "rails", "2.3.8"
gem "activesupport", "2.3.8", :require => "active_support"
gem "rake", "0.8.7"

# might be for POW, not sure
gem "childprocess", "0.2.2", :git => "git://github.com/jarib/childprocess.git"


# app
gem "mysql", "2.7"
gem "lockfile"
gem "ssl_requirement", "0.1.0"
gem "attr_encrypted", "1.1.2"
#gem "searchlogic", "2.5.6"
gem "will_paginate", "2.3.16"
gem "paperclip"


gem "garb", "0.7.6"
gem "delayed_job", "2.0.3"
gem "httparty", "0.5.2"


gem "pony", "1.1"
gem "friendly_id", "3.0.6"
gem "stringex", "1.1.0"
gem "i18n", "0.4.2"

gem "sms_fu", "1.1.1"

NOTE: for some reason search logic only works when it's included in the Rails:Initializer.run call in classic rails 2.3 way., config.gem "searchlogic", :version => "2.2.28"

回答1:

Disable the auto-requiring of will_paginate:

# Gemfile
gem "will_paginate", "~> 2.3.15", :require => nil

And require it yourself, manually, at the end of environment.rb:

# end of environment.rb
require "will_paginate"

The thing is, if you require will_paginate before the Rails::Initializer call, it might not boostrap properly.