Rails Kaminari 分页出现 undefined method `current_page' for #<ActiveRecord::Relation:0x69041c9b2d58>

runup · 2013年09月11日 · 最后由 runup 回复于 2013年09月11日 · 3432 次阅读

源码https://github.com/runup520/cims_one 环境:环境:ubuntu13.04 sqlite3 ruby1.9.3 rails3.2.13 启动语句 rake sunspot:solr:start rale snspot:reindex rails s 在 action 中添加

@jobs = Article.where("tag = '就业信息'").page(params[:page]).per(1)

在 view 中添加

<%=paginate @jobs, :theme => 'twitter-bootstrap' %>  

出现这个错误

undefined method `current_page' for #<ActiveRecord::Relation:0x69041c9b2d58>

参考http://stackoverflow.com/questions/11200330/kaminari-rails-pagination-undefined-method-current-page 没有解决

求问各位前辈

contorller 中分页的对象时@Jobs 页面中调用的却是 @articles 肯定出错啊

<%=paginate @jobs , :theme => 'twitter-bootstrap' %>

#1 楼 @shawnyu 不好意思。我笔误,我原来的代码中是

<%=paginate @jobs, :theme => 'twitter-bootstrap' %> 

文中已经做了修改。出现的还是上文提到的问题。感谢前辈指点。

#2 楼 @ruby_sky 对不起,这是我笔误。源码中是 jobs,我抄错了。还是出现这个问题。请前辈指点。

#4 楼 @runup :theme => 'twitter-bootstrap' 尝试把这个去掉。看行不行。

params[:page] 为空的考虑过了?,,,这个没关系。忽略 views 里面的变量和 controller 里面对应没错哦,去 debug @jobs变量本身看看是否 ok

检查下是不是 Gemfile 里有 will_paginate

顺道,controller 里面重复定义十几个类似的实例变量可以考虑重构

#8 楼 @as181920 我是新手,不知道如何 controller 进行重构,只知道 view 的

#6 楼 @as181920 恩,问题解决了,但是不知道如何为什么解决的。

#7 楼 @jjym 这个没有的

#5 楼 @ruby_sky 问题解决了,当时不是这个原因。我也不清楚这个问题的愿意在那里/。 参考http://stackoverflow.com/questions/11200330/kaminari-rails-pagination-undefined-method-current-page

#12 楼 @runup 你的问题我拉了你的代码都重现了,就是 @jobs 写成了 @articles..如果写对了,分页是正常的。

#13 楼 @ruby_sky 恩。问题解决了,应该就是代码写错了。非常感谢前辈

需要 登录 后方可回复, 如果你还没有账号请 注册新账号