3

我得到了

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

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

康康代码:

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

我的应用程序的规格

  • 导轨 3.0.3
  • 设计 1.5.3
  • 设计邀请 0.6.0
  • 康康 1.6.4
4

1 回答 1

5

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

于 2012-12-21T15:26:45.087 回答