Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

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

Closed
sonymony opened this Issue · 13 comments

8 participants

@sonymony

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

 
    
  

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: 

2: @article%> 3: 4: 5: 6: 7:

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

load_and_authorize_resource

as such

class ArticlesController  [: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..

@ryanb
Owner

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?

@sonymony

I am running Rails 3

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

@ryanb
Owner

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

@sonymony

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

@sonymony

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

@ryanb
Owner

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
Owner

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.

@gumho

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

@archanasingh

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

@rajamohamed-mjh

thanks ! me toooo

@jackquack

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

@jess

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

@arelem3

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
.

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.