本文介绍了kaminari 未定义的方法`total_pages'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在使用 kaminari 时,我遇到了一个错误.
宝石文件:
# gem 'will_paginate', '~>3.0.6'# gem 'will_paginate-bootstrap'宝石 'kaminari'
lists_controller.rb
定义索引如果参数[:tag]@lists = List.tagged_with(params[:tag]).order(created_at: :desc).paginate(page:params[:page], per_page: 3 )别的@lists = List.all.order(created_at: :desc)结尾结尾
我也使用 .page params[:page].per(2)
按照 .order(created_at: :desc)
但不工作
views/lists/index.html.erb
错误在这里
未定义的方法`total_pages' #提取的源代码(围绕第 26 行):
<div class="容器"><div class="row"><div class="col-md-8">
我正在关注关于 kaminari 的 railscasts 视频,但他们没有任何错误.
解决方案
您需要对两个查询进行分页.我推荐类似的东西:
def 索引如果参数[:tag]@lists = List.tagged_with(params[:tag])别的@lists = List.all结尾@lists = @lists.order(created_at: :desc).paginate(page:params[:page], per_page: 3 )结尾
否则当 params[:tag]
为零时,@lists
将不是分页对象.
While using kaminari, I got an error.
Gemfile:
# gem 'will_paginate', '~> 3.0.6'
# gem 'will_paginate-bootstrap'
gem 'kaminari'
lists_controller.rb
def index
if params[:tag]
@lists = List.tagged_with(params[:tag]).order(created_at: :desc).paginate(page:params[:page], per_page: 3 )
else
@lists = List.all.order(created_at: :desc)
end
end
I also user .page params[:page].per(2)
follow .order(created_at: :desc)
but not work
views/lists/index.html.erb
<%= paginate @lists %>
the error is here
undefined method `total_pages' for #<List::ActiveRecord_Relation:0x007fa2303e3fa8>
Extracted source (around line #26):
</div>
</div>
<%= paginate @lists %>
<div class="container">
<div class="row">
<div class="col-md-8">
I was following a railscasts video about kaminari, but they did not have any error.
解决方案
You need to paginate both queries. I reccomend something like:
def index
if params[:tag]
@lists = List.tagged_with(params[:tag])
else
@lists = List.all
end
@lists = @lists.order(created_at: :desc).paginate(page:params[:page], per_page: 3 )
end
Otherwise @lists
will not be a pagination object when params[:tag]
is nil.
这篇关于kaminari 未定义的方法`total_pages'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!