按照https://github.com/amatsuda/kaminari的步骤安装了Kaminari,
gem 'kaminari'
bundle
而且用bundle show kaminari
可以查看到安装地址
/Users/tinyfool/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/kaminari-0.16.1
在代码里面
def index
#@demos = Demo.all
@demos = Demo.page(params[:page])
end
但是页面报错为:
undefined method `page' for #<Class:0x007fe3b0874240>
Extracted source (around line #5):
def index
#@demos = Demo.all
@demos = Demo.page(params[:page])
end
Rails.root: /Users/tinyfool/work/democodes
这种情况该如何处理呢?
刚才有人提示我重新启动服务器,我测试了一下发现,有gem 'kaminari'
在,服务器就启动不起来,报错为:
rails s
bin/rails:6: warning: already initialized constant APP_PATH
/Users/tinyfool/work/democodes/bin/rails:6: warning: previous definition of APP_PATH was here
去掉,就可以启动,汗
你的系统上可能安装了不同版本的 Rails 或者 某个 gem 的多个版本,导致 rails s
的时候警告,试试看用
bundle exec rails s
或者
./bin/rails s
来启动,你安装 kaminari
的方式没有问题,可以试试看grep搜一下你的应用中什么地方定义了这个 APP_PATH
或者这个被override了。另外一个简单的办法就是在 rails 的 console 里面测试,Rails 的 console 就是一个完整的 rails 命令行环境
~/foobar $ ./bin/rails c
Loading development environment (Rails 4.1.1)
>> Demo.page(3)
Demo Load (1.2ms) SELECT "demos".* FROM "demos" LIMIT 25 OFFSET 50
=> #<ActiveRecord::Relation []>
>>
6楼 @lgn21st APP_PATH
只出现在了./bin/rails
里
./bin/rails c的错误信息是
./bin/rails c
/Users/tinyfool/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/kaminari-0.16.1/lib/kaminari/sinatra.rb:1:in `require': cannot load such file -- sinatra/base (LoadError)
from /Users/tinyfool/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/kaminari-0.16.1/lib/kaminari/sinatra.rb:1:in `<top (required)>'
from /Users/tinyfool/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/bundler-1.6.2/lib/bundler/runtime.rb:76:in `require'
from /Users/tinyfool/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/bundler-1.6.2/lib/bundler/runtime.rb:76:in `block (2 levels) in require'
from /Users/tinyfool/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/bundler-1.6.2/lib/bundler/runtime.rb:72:in `each'
from /Users/tinyfool/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/bundler-1.6.2/lib/bundler/runtime.rb:72:in `block in require'
from /Users/tinyfool/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/bundler-1.6.2/lib/bundler/runtime.rb:61:in `each'
from /Users/tinyfool/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/bundler-1.6.2/lib/bundler/runtime.rb:61:in `require'
from /Users/tinyfool/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/bundler-1.6.2/lib/bundler.rb:132:in `require'
from /Users/tinyfool/work/democodes/config/application.rb:7:in `<top (required)>'
from /Users/tinyfool/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/spring-1.1.3/lib/spring/application.rb:82:in `require'
from /Users/tinyfool/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/spring-1.1.3/lib/spring/application.rb:82:in `preload'
from /Users/tinyfool/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/spring-1.1.3/lib/spring/application.rb:140:in `serve'
from /Users/tinyfool/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/spring-1.1.3/lib/spring/application.rb:128:in `block in run'
from /Users/tinyfool/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/spring-1.1.3/lib/spring/application.rb:122:in `loop'
from /Users/tinyfool/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/spring-1.1.3/lib/spring/application.rb:122:in `run'
from /Users/tinyfool/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/spring-1.1.3/lib/spring/application/boot.rb:18:in `<top (required)>'
from /Users/tinyfool/.rbenv/versions/2.1.2/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from /Users/tinyfool/.rbenv/versions/2.1.2/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from -e:1:in `<main>'
看着意思是说,我缺了sinatra这个东西?汗,这又是什么?
12楼 @tinyfool 是的呀,不应该这么写,因为你并没有用到 Sinatra,https://github.com/amatsuda/kaminari 在 Github 的 README 上介绍的用法足够简单了,我是翻了一下源码看到的:
https://github.com/amatsuda/kaminari/blob/master/lib/kaminari.rb#L11
$stderr.puts <<-EOC if !defined?(Rails) && !defined?(Sinatra) && !defined?(Grape)
warning: no framework detected.
Your Gemfile might not be configured properly.
---- e.g. ----
Rails:
gem 'kaminari'
Sinatra/Padrino:
gem 'kaminari', :require => 'kaminari/sinatra'
Grape:
gem 'kaminari', :require => 'kaminari/grape'
EOC