Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

undefined local variable or method `current_user' for

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
like image 730
Tarscher Avatar asked Jun 26 '26 21:06

Tarscher


1 Answers

Maybe you are falling to this little detail about cancan and devise. https://github.com/ryanb/cancan/wiki/changing-defaults
Cancan expects a current_user method to be in your controller. Check to see if this exists.

like image 158
MurifoX Avatar answered Jul 01 '26 01:07

MurifoX