我从头构建用户授权并一直在处理用户角色。我被困在权限部分,因为我不知道如何定义它。当我向控制器添加“before_filter:authorize”时,它会在所有页面上显示“未初始化的常量ApplicationController :: Permission”错误。如何为用户角色定义权限
我知道错误是从我application_controller.rb文件蒸:
def current_permission
@current_permission || Permission.new(current_user)
end
def authorize
if !current_permission.allow?(params[:controller], params[:action])
redirect_to root_url, alert: "Not authorized."
end
Permission.rb:
Class Permission < Struct.new(:user)
def allow?(controller, action)
if user.nil?
controller == "galleries" && action.in?(%w[index show])
elsif user.admin?
true
else
controller == "galleries" && action != "destroy"
end
end
我不知道的是如何在我的应用程序正确地定义权限这样我就不会收到那个错误。有没有人有任何想法?
权限是一个ActiveRecord模型吗?还是一个简单的对象?你在哪里定义了它? – pierallard 2013-03-14 14:50:46
我刚刚添加了我的permission.rb文件。 – pwz2000 2013-03-14 14:52:02
是的,请参阅下面的答案。你**肯定**应该看看** [学者](https://github.com/elabs/pundit)** – deefour 2013-03-14 14:52:15