2012-03-06 74 views
1

我正在使用cancan作爲權限,因此用戶在訪問其個人資料/用戶頁面時看不到其他用戶。用戶應該只能看到自己。坎康用戶權限?

在我ability.rb文件我有

class Ability 
include CanCan::Ability 

def initialize(user) 
    user ||= User.new # guest user (not logged in) 

    if user.role? :admin 
    can :manage, :all 
    elsif user.role? :rookie 
    can [:update, :destroy], [Album, Photo, User] 
    can :read, :all 
    end 

    can :manage, Album, :profile => { :user_id => user.id } 
    can :manage, Photo, :profile => { :user_id => user.id } 
    can :manage, Video, :profile => { :user_id => user.id } 
    can :manage, Comment, :blog => { :profile => { :user_id => user.id } } 
    can :manage, User, :id => user.id 
end 
end 

在我users_controller我有

class UsersController < ApplicationController 
before_filter :authenticate_user! 
load_and_authorize_resource 

def index 
    @user = current_user 
end 

def show 
    @user = User.find(params[:id]) 
end 
end 

上述通常工作,但由於用戶是主要模式,我不知道我該怎麼解決這個。 Rails的給我的錯誤

undefined method `user_id' 

回答

1

它應該是:

#ability.rb: 
can :manage, User, :id => user.id 
+0

工作,但我能夠看到其他用戶?有什麼建議麼? – coletrain 2012-03-06 14:39:26

+0

你的角色是什麼? – 2012-03-06 15:01:15

+0

我的角色是菜鳥和管理員 – coletrain 2012-03-06 15:15:49

1

我假設大多數用戶開始使用角色:新秀,你必須與用戶的能力

can :read, :all 

角色:菜鳥。然後這意味着所有:菜鳥用戶將能夠讀取 所有資源。