我一直在使用康康宝石的railscast,但我坚持如何只允许用户访问他们自己的显示页面。我如何允许用户只使用cancan访问他们自己的显示页面?
我的代码如下所示:
能力模型
class Ability
include CanCan::Ability
def initialize(user)
user ||= User.new # guest user (not logged in)
if user.role == "admin"
can :manage, :all
else
can :read, :all
if user.role == "author"
can :create, Review
can :update, Review do |review|
review.try(:user) == user
end
can :update, User do |user|
user.try(:current_user) == current_user
end
end
if user.role == "owner"
can :update, Venue
end
end
end
end
用户控制器
class UsersController < ApplicationController
load_and_authorize_resource
end
用户(作者)只能更新thier自己的意见,但能目前通过更改URL来查看所有用户显示页面。
我在这里错过了什么?
非常感谢任何帮助,非常感谢!
问题出在'can:read,:all'这一行。您允许查看所有用户的所有页面。 – mirelon 2014-11-13 13:28:18