有没有一种方法可以在application_controller中添加一些代码,以便Rails可以使用cancan自动保护每个控制器,而不必在每个控制器中添加load_and_authorize_resource
?
Yes, you can use inheritance:
class HomeController < ApplicationController
# [etc]
end
class SettingsController < ApplicationController
end
class UsersController < SettingsController
end
class ProductsController < SettingsController
end
So you can give access to the SettingsController:
can :manage, SettingsController
Which will give access to all controllers that inherit from SettingsController.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.