真的很困惑。角色建立並且在one role per user method之後很好地工作。我的用戶模式如下。每個用戶一個角色CanCan,未定義的方法「角色」問題
class User < ActiveRecord::Base
ROLES = %w[admin landlord]
def role?(role)
roles.include? role.to_s
end
end
這是當我將權限添加到我的能力模型我得到以下錯誤。
undefined method `role' for #<ActionDispatch::Session::AbstractStore::SessionHash:0x10433a5e0>
我的能力模型如下。
class Ability
include CanCan::Ability
def initialize(user)
if user.role == "admin"
can :manage, :all
else
can :read, :all
end
end
end
這裏是我在終端中看到的。
NoMethodError (undefined method `role' for #<ActionDispatch::Session::AbstractStore::SessionHash:0x1044d46a8>):
app/models/ability.rb:5:in `initialize'
app/controllers/application_controller.rb:6:in `new'
app/controllers/application_controller.rb:6:in `current_ability'
正如你可以告訴我只是學習!即使向正確的方向推動也會令人驚歎。謝謝。
我猜有一個與CURRENT_USER值的問題。你在使用設計嗎?你在current_ability中定義了什麼? – e3matheus
我沒有使用Devise,只是編寫了我擁有的Rails 3書籍的登錄名。 def current_ability @ current_ability || = Ability.new(session) end – Delete