#<ActiveRecord::Relation (Kaminari gem)

问题描述 投票:0回答:1

我不明白为什么 Kaminari 不工作,我有

admins_controller.rb

class AdminsController < ApplicationController
    before_action :authenticate_admin!
    

    def index
        # Admin access to the order tables
        @orders = Order.all.page(params[:page]).per(5)
        @q = Order.ransack(params[:q])
        @orders = @q.result.order(created_at: :desc)
    end

    
end

views/admins/index.html.erb

</table>
  
    <div class="paginator">
      <%= paginate @orders %>
    </div>

上面的代码根本不能与 kaminari 一起工作,并给出了一个错误

`undefined method `total_pages' for #<ActiveRecord::Relation [#<Order id: 159......`

但是,我还有另一个带有自定义范围的控制器和索引页面,效果非常好

second_admins_controller.rb

class SecondAdminsController < ApplicationController
    before_action :authenticate_admin!

    def index
        @picks = Pick.where(shop:"Sapna Xerox Near PDA College").order(created_at: :desc).page(params[:page]).per(5)
        @q = Order.ransack(params[:q])
        @orders = @q.result.order(created_at: :desc)
    end
end

views/second_admins/index.html.erb

</table>
    <div class="paginator">
      <%= paginate @picks %>
    </div>

这个

second_admins_controller.rb
和它的
index.html.erb
有分页,它完美地工作而
admins_controller.rb
和它的
index.html.erb
抛出一个错误! 任何解决方案? 我尝试查看其他 Stackoverflow 解决方案,但没有一个有帮助!

ruby-on-rails ruby ruby-on-rails-7 kaminari
1个回答
0
投票

你重新定义了

@orders
变量,所以它在没有
.page(params[:page]).per(5)

的情况下使用

你可以试试这个

def index
    # Admin access to the order tables
    @q = Order.ransack(params[:q])
    @orders = @q.result.order(created_at: :desc).page(params[:page]).per(5)
end
© www.soinside.com 2019 - 2024. All rights reserved.