0

Am using cancancan for authorization.And am using will_paginate for table pagination. Its works fine until I add load_and_authorize_resource in controller. When using load_and_authorize_resource in controller, will_paginate throws 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

Controller:

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

View:

<% if @assignments != nil then%>
    <%= will_paginate @assignments, :class => @paginationClass.to_s,  renderer: BootstrapPagination::Rails %>
    <%end%>
Raj
  • 950
  • 1
  • 9
  • 33
  • possible duplicate of [will\_paginate -error-undefined method \`total\_pages'](http://stackoverflow.com/questions/6356062/will-paginate-error-undefined-method-total-pages) – Pavan Jun 06 '15 at 12:17

2 Answers2

0

This error appear when your @assignments is empty because in this case your code doesn't call paginate method. So, you should call pagination method always to avoid this problem (in your case just remove this useless if).

  • After I remove if conditions and call paginate method always but still throws same error@lsabel Stracke – Raj Jun 08 '15 at 04:33
0

Change the object name used for pagination .

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

and in your view like this

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

This will fix your issue

Suresh Kumar
  • 227
  • 1
  • 11
  • 29