Skip to content


undefined method `can?' for #<#<Class:0x00000101e0cae8>:0x00000101e04e88> #124

sonymony opened this Issue · 14 comments

9 participants


I am using devise & cancan for authentication/authorization

I have had success with just devise but when I try to apply authorization with cancan I get errors

I do the following
1. create a user-role as suggested by

My models/ability.rb is as follows

class Ability
  include CanCan::Ability

  def initialize(user)
    user ||= # in case of guest
    if user.has_role? :admin
      can :manage, :all
      can :read, :all

Then I update the views/ as such

 <% if can? :update, @article %>
    <%= link_to "Edit", edit_article_path(@article) %>
  <% end %>

Then when I go to http://localhost:3000/articles/1

I get the following error

undefined method `can?' for #<#:0x00000101e04e88>

Extracted source (around line #4):


<%= notice %>

2: <%= render :partial =>@article%> 3: 4: <% if can? :update, @article %> 5: <%= link_to "Edit", edit_article_path(@article) %> 6: <% end %> 7: <%= link_to 'Back to Article..', articles_path %>

Then when I add the follwoing to app/controllers/articles_controllers.rb


as such

class ArticlesController < ApplicationController
  before_filter :authenticate_user!, :except => [:show, :index]

I get the following error at http://localhost:3000/

undefined local variable or method `load_and_authorize_resource' for ArticlesController:Class

Please help...
I have included
gem 'cancan' in Gemfile & have run the command $ bundle install
When I do Bundle Show... it says
the follwoing as present

  • cancan (1.3.2)
    • devise (1.1.1)

PLEASE help... I have been trying to solve this for 1 week..


It sounds like CanCan is not really being included, but it should be if you have it mentioned in the Gemfile. What version of Rails are you using?

Also, what happens when you run rails console and type CanCan. Does it give an error saying unknown class/module?


I am running Rails 3

$ rails console
ruby-head > CanCan
=> CanCan
ruby-head >


Which version of Rails 3? I'm wondering if there's a specific release that does not load it for some reason.


$ rails -v
Rails 3.0.0.rc
$ ruby -v
ruby 1.9.2dev (2010-05-07 trunk 27649) [x86_64-darwin10.3.0]


Is this problem specific to Rails 3.0.0.rc ?


I just tested this and it is working for me in Rails 3.0.0.rc so the problem isn't there. Double check your Gemfile and make sure you are using gem "cancan" and not require "cancan" there. Also be sure that line is below the Rails gem.

Here's the steps I took to try to duplicate the problem.

rails new cantest
cd cantest
rails g scaffold project name:string
rake db:migrate

# bottom of Gemfile
gem "cancan"

# in models/ability.rb
class Ability
  include CanCan::Ability
  def initialize(user)
    can :manage, :all

# in application_controller.rb
  def current_user

# in projects_controller.rb

Going to the /projects url worked without error, so perhaps there is something specific about your environment or application?


I haven't heard any further reports of this so I'm closing. If you're still experiencing an issue with this, comment here and I'll re-open.


Btw, I ran into this issue too but it was solved by restarting WEBrick.


Thanks gumho! I got same error and restarting WEBrick resolved the issue.


thanks ! me toooo


I was having this problem too. But the fault was that of the Cell gem.


If you're using unicorn/nginx, I had to manually stop unicorn and start back again. A restart (performed by capistrano) apparently wasn't good enough??


Doesn't work in Rails 4.
Used a combination of Agile rails and Ryan Bate's tutorials to create a small dedicated Registrations system which works well, much thanks to Ryanb.

from the info above: load_and_authorize_resource gives error: undefined method `role?' for true:TrueClass

Following Robert Huberdeau's blog tutorial I have 3 roles.
ability.rb defines what each role may or may not do:
class Ability < ActiveRecord::Base
include CanCan::Ability

def initialize(user)
    user ||= # guest user
    if user.role? :SiteAdmin
        can :manage, :all
        can :publish, :all
    elsif user.role? :RegistrationAdmin
        can :publish, :all
    elsif user.role? :Member
        can :create,[:Registration]
        can :read,[:Registration, :Line_item]
        can :edit, :update[:Registration, :Line_item]


in Registrations controller:
authorize! :edit, @registrations
authorize! :create, @registrations
authorize! :update, @registrations
undefined method `authorize!' for RegistrationsController:Class


Restart server rails, I will be work fine !

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.