I’m getting the
undefined local variable or method `current_user` error for
caused by the following code in my view
<% if can? :update, Project %>
<span class="admin">
<%= link_to 'Nieuw Project', new_project_path %>
</span>
<% end %>
When I remove the code the page renders correctly.
CanCan code:
class Ability
include CanCan::Ability
def initialize(user)
user ||= User.new
if user.admin?
can :manage, Post
can :manage, Project
can :manage, Page
can :manage, Comment
can :manage, User
else
can :read, :all
cannot :read, User
can :create, Comment
end
end
end
User model:
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :lockable and :timeoutable
devise :invitable, :database_authenticatable, #:registerable, :confirmable,
:recoverable, :rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation
def admin?
self.id
end
end
specs of my app
- rails 3.0.3
- devise 1.5.3
- devise invitable 0.6.0
- cancan 1.6.4
Maybe you are falling to this little detail about cancan and devise. https://github.com/ryanb/cancan/wiki/changing-defaults
Cancan expects a
current_usermethod to be in your controller. Check to see if this exists.