cancan icon indicating copy to clipboard operation
cancan copied to clipboard

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

Open sonymony opened this issue 16 years ago • 14 comments

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 http://www.tonyamoyal.com/2010/07/28/rails-authentication-with-devise-and-cancan-customizing-devise-controllers/

My models/ability.rb is as follows

class Ability
  include CanCan::Ability

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

Then I update the views/articles.show.html.erb 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):

1: 

<%= 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

load_and_authorize_resource

as such

class ArticlesController < ApplicationController
  load_and_authorize_resource
  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..

sonymony avatar Aug 17 '10 18:08 sonymony

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?

ryanb avatar Aug 17 '10 18:08 ryanb

I am running Rails 3

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

sonymony avatar Aug 17 '10 19:08 sonymony

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

ryanb avatar Aug 17 '10 20:08 ryanb

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

sonymony avatar Aug 17 '10 20:08 sonymony

Hello.... Is this problem specific to Rails 3.0.0.rc ?

sonymony avatar Aug 18 '10 18:08 sonymony

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
  end
end

# in application_controller.rb
  private
  def current_user
    true
  end

# in projects_controller.rb
load_and_authorize_resource

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

ryanb avatar Aug 18 '10 23:08 ryanb

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.

ryanb avatar Aug 30 '10 20:08 ryanb

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

gumho avatar Feb 01 '11 06:02 gumho

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

archanasingh avatar Sep 01 '11 18:09 archanasingh

thanks ! me toooo

rajamohamed-mjh avatar Dec 09 '11 10:12 rajamohamed-mjh

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

hrdwdmrbl avatar Apr 25 '12 16:04 hrdwdmrbl

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??

jess avatar Jul 11 '12 03:07 jess

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 ||= User.new # 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]
        end
end

end

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

arelem3 avatar Sep 08 '13 09:09 arelem3

Restart server rails, I will be work fine !

binhht-evolableasia avatar Oct 21 '15 09:10 binhht-evolableasia