undefined局部变量或方法`current_user'for

时间:2012-12-21 15:12:39

标签: ruby-on-rails-3 device cancan

我正在接受

undefined local variable or method `current_user` error for

由我视图中的以下代码引起

<% if can? :update, Project %>
  <span class="admin">
    <%= link_to 'Nieuw Project', new_project_path %>
  </span>
<% end %>

当我删除代码时,页面呈现正确。

CanCan代码:

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

用户模型:

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

我的应用的规格

  
      
  • rails 3.0.3
  •   
  • 设计1.5.3
  •   
  • 设计邀请0.6.0
  •   
  • cancan 1.6.4
  •   

1 个答案:

答案 0 :(得分:5)

也许你正在接受关于康康和设计的这个小细节。 https://github.com/ryanb/cancan/wiki/changing-defaults
Cancan希望current_user方法在您的控制器中。检查是否存在。