我正在使用WillPaginate Ruby gem v3.1.6
。
鉴于您可以运行@posts = Post.all.paginate(page: params[:page])
或@posts = Post.all
,有没有一种方法可以检查@posts
当前是否在分页?也就是说,@posts
是否正在运行/使用WillPaginate?
我想要做的事情如下:
if @posts.will_paginate? # or @posts.paginated?
# paginated @posts
else
# not paginated @posts
通过查看宝石source code,我会使用...
def will_paginate? # or paginated?
@posts.respond_to?(:total_pages)
end
...或以某种方式检查WillPaginate
是@posts
的类祖先,但是也许有更好的方法来检查记录是否被分页。
答案 0 :(得分:0)
def paginated?
@posts.count > WillPaginate.per_page
end
您可以通过per_page
在Post模型上,本地或全局设置WillPaginate.per_page
。
讨论后编辑
model / car.rb
class Car
def paginated?
false
end
end
model / post.rb
class Post
def paginated?
true
end
end
OR
views / shared / _universal.html.erb
<!-- renders your universal layout for the number of objects -->
<% @items.each do |item| %>
<div><%= item.property %></div>
<% end %>
views / cars / index.html.erb
...
<%= render partial 'shared/_universal.html.erb', locals: { items: @cars } %>
views / posts / index.html.erb
...
<%= render partial 'shared/_universal.html.erb', locals: { items: @posts } %>
<%= will_paginate @posts %>