我正在使用rails 3.2.我使用.page(1).per_page(10)对结果进行分页
喜欢
@users = User.method().page(1).per_page(10)
Run Code Online (Sandbox Code Playgroud)
现在如何从分页中查找用户的总数
因为因为从第一页@users.count
得到10而不是总计数
即使在分页后如何获得用户的总数
编辑:@users.total_count
给出整个分页计数
为了对一个公共阵列进行分页,我得到了这个解决方案
@arr_name = Kaminari.paginate_array(@arr_name).page(params [:page]).per(PER_PAGE_RECORDS)
PER_PAGE_RECORDS
是一个根据分页需要的值的变量.
任何更好的想法?
也有一个使用分页的ajax调用可以使用这个,
在你看来,
将id添加到div选项卡
div id="paginate"
Run Code Online (Sandbox Code Playgroud)
在里面
<%= paginate @arr_name,:remote => true%>
并在js响应文件中,
$('#paginate').html('<%= escape_javascript(paginate(@arr_name,:remote => true).to_s)%>');
所以你的请求将是AJAX.
谢谢.
我有一个@ level1数组,如下所示:
[[3.0, 4, 2], [2.0, 48, 3], [2.1, 56, 4], ............]
Run Code Online (Sandbox Code Playgroud)
我想在这个数组上应用分页,这样每个页面一次只显示几行.我试过这个:
@temp1 = @level1.paginate(:page => params[:page])
Run Code Online (Sandbox Code Playgroud)
但它会引发以下错误:
undefined method `paginate' for [[3.0, 4, 2], [2.0, 48, 3], [2.1, 56, 4]]:Array
Run Code Online (Sandbox Code Playgroud)
如何使用will_paginate对此进行分页?
我搜索和搜索,但没有解决我的问题.这是我的控制器:
def show
@topic = Topic.find(params[:id])
@topic.posts = @topic.posts.page(params[:page]).per(2) # 2 for debugging
end
Run Code Online (Sandbox Code Playgroud)
功能很好,因为主题视图减少到两个帖子.但是,当我将此添加到show.html.erb时:
<%= paginate @topic.posts %>
Run Code Online (Sandbox Code Playgroud)
我给出了这个错误:
undefined method `current_page' for #<ActiveRecord::Relation:0x69041c9b2d58>
Run Code Online (Sandbox Code Playgroud) 我无法超越这个.我知道我已经读过没有数组的页面方法,但我该怎么办?
如果我在控制台中运行Class.all,则返回#,但如果我运行Class.all.page(1),则会出现上述错误.
有任何想法吗?
我有一个视图规格正在通过,但现在已经破坏了分页(通过Kaminari宝石)已被添加到视图中.我仍然试图了解RSpec的语法...所以寻找帮助来实现这一点,因为页面在浏览器中工作正常.我知道有很多人对观察规格不屑一顾,因为它很脆弱(可能出于这样的原因),但我还是希望保持这一点.
我正在为@posts数组分配一些存根帖子.但阵列没有响应current_page
.那我该如何在RSpec中处理这个呢?
Failures:
1) posts/index.html.haml renders a list of posts
Failure/Error: render
ActionView::Template::Error:
undefined method `current_page' for #<Array:0x000001028ab4e0>
# ./app/views/posts/index.html.haml:31:in `_app_views_posts_index_html_haml__291454070937541541_2193463480'
# ./spec/views/posts/index.html.haml_spec.rb:39:in `block (2 levels) in <top (required)>'
Run Code Online (Sandbox Code Playgroud)
spec/views/posts/index.html.haml_spec.rb
:
require 'spec_helper'
describe "posts/index.html.haml" do
before(:each) do
...
assign(:posts, [
Factory.stub(:post),
Factory.stub(:post)
])
view.should_receive(:date_as_string).twice.and_return("June 17, 2011")
...
end
it "renders a list of posts" do
render
rendered.should have_content("June 17, 2011")
...
end
end
Run Code Online (Sandbox Code Playgroud) 我正在创建一个搜索页面,可以对用户,帖子和评论进行应用程序范围的搜索.我目前有:
# POST /search
def index
query = params[:query]
@users = User.search(query).page(params[:page])
@posts = Post.search(query).page(params[:page])
@comments = Comment.search(query).page(params[:page])
respond_to do |format|
format.html
end
end
Run Code Online (Sandbox Code Playgroud)
然而,我真的想要得到一些所有结果混合在一起然后分页的东西.这样做分页搜索的策略是什么?谢谢!
我正在迭代大量的模型对象,需要为性能/内存原因进行分页.
我想做这样的事情:
for i in 1..Person.num_pages
Person.page(i).each do
# work
end
end
Run Code Online (Sandbox Code Playgroud)
显然我可以自己计算并做数学,但是有相当于num_pages吗?或者是否有更优雅的方式完成这项工作?
我们正在从Rails 4.2.5升级到5.0.0.beta1
在测试时,我们希望看到像以前一样使用分页链接呈现的索引视图.但是我们现在得到一个ArgumentError错误页面,例如:
ArgumentError in Transactions#index
/app/views/kaminari/_paginator.html.erb where line #10 raised:
<%= paginator.render do -%>
Generating an URL from non sanitized request parameters is insecure!
Application Trace | Framework Trace | Full Trace
app/views/kaminari/_paginator.html.erb:10:in block in _app_views_kaminari__paginator_html_erb___4026289994022119719_69904100316060' app/views/kaminari/_paginator.html.erb:9:in_app_views_kaminari__paginator_html_erb___4026289994022119719_69904100316060'
app/views/transactions/index.html.erb:2:in `_app_views_transactions_index_html_erb__422882858554400818_60602560'
Run Code Online (Sandbox Code Playgroud)
kaminari提出了一个问题
进一步研究这里是新的Rails 5.0.0.beta1代码,现在抛出错误:
将它添加到config/application.rb'修复'它,但不是一个好主意:
config.action_controller.permit_all_parameters = true
Run Code Online (Sandbox Code Playgroud)
而是添加这个并不能解决问题,不确定原因:
config.action_controller.always_permitted_parameters = [:current_page, :page, :total_pages, :per_page, :remote, :paginator]
Run Code Online (Sandbox Code Playgroud) 我理解Kaminari
Rails3阅读本文的表现很好:Rails 3分页,will_paginate与Kaminari,但Rails4怎么样?另外,使用Bootstrap3对它们进行样式化时,哪种gem更容易解决?
pagination ruby-on-rails will-paginate kaminari twitter-bootstrap