0

我正在使用 cancancan 进行授权。并且正在使用 will_paginate 进行表分页。它工作正常,直到我在控制器中添加 load_and_authorize_resource 。在控制器中使用 load_and_authorize_resource 时,will_paginate 会抛出ActionView::Template::Error (undefined methodtotal_pages' for #)`: Abilyty.rb:

def initialize(user)    
    if user.user_type == "ADMIN" then
      can :manage, :all
      cannot :manage, ParentMessageController
    elsif user.user_type == "MANAGEMENT" then
      can :manage, :all
      cannot :manage, ParentAttendance
end

控制器:

class AssignmentsController < ApplicationController  
  before_action :set_assignment, only: [:show, :edit, :update, :destroy]
  load_and_authorize_resource
def index
 getAssignments
end

def getAssignments
@assignments = Assignment.all
if (@assignments != nil && @assignments.length > 0) then                                                           
     @assignments = @assignments.paginate(:per_page => 5, :page => params[:page])
    end
  end

看法:

<% if @assignments != nil then%>
    <%= will_paginate @assignments, :class => @paginationClass.to_s,  renderer: BootstrapPagination::Rails %>
    <%end%>
4

2 回答 2

0

当您的 @assignments 为空时会出现此错误,因为在这种情况下您的代码不会调用paginate方法。因此,您应该始终调用 pagination 方法以避免此问题(在您的情况下,只需删除这个无用的if)。

于 2015-06-06T12:32:23.003 回答
0

更改用于分页的对象名称。

def getAssignments
@assignments = Assignment.all
if (@assignments != nil && @assignments.length > 0) then                                                           
     @assignment = @assignments.paginate(:per_page => 5, :page => params[:page])
    end
  end

在你看来像这样

<% if @assignment != nil then%>
    <%= will_paginate @assignment, :class => @paginationClass.to_s,  renderer: BootstrapPagination::Rails %>
    <%end%>

这将解决您的问题

于 2015-06-08T04:54:49.910 回答