我想“映射”MyController
到User
模型
#ability
class Ability
include CanCan::Ability
def initialize user
user ||= User.new
if user.has_role? :admin
can :manage, :all
else
can :read, :all
can :read, User do |u|
u && u.user_id == user.id
end
end
end
end
#routes
get 'my_controller/show/(:id)', to: 'MyController#show'
#controller
class MyController < ApplicationController
# load_and_authorize_resource
def show
# showing a user
end
end
所以my_controller/show/(:id)
只能对admin
和访问current user
。例如,用户id == 2
可以看到自己的个人资料my_controller/show/2
,而不能看到其他用户的个人资料,例如my_controller/show/1234
除非他们是admin
.
项目中没有My
模型。理想情况下,我必须重命名MyController
为,UserController
但由于某种原因我不允许这样做。
如果我load_and_authorize_resource
在 处取消注释MyController
,则会出现以下错误
NameError in MyController#show
uninitialized constant My
那么我怎样才能摆脱它呢?