Ruby on rails 在行动之前定义个人
我想在过滤之前定义一个Ruby on rails 在行动之前定义个人,ruby-on-rails,ruby-on-rails-4,Ruby On Rails,Ruby On Rails 4,我想在过滤之前定义一个,就像身份验证\u用户在设计中。在这个项目中,我有一个用户模型,该模型由设计和角色生成,以设置用户角色。现在,我想在过滤器之前创建一个,,检查用户对reach out controller的权限。以下代码显示模型: user.rb: class User < ActiveRecord::Base devise :database_authenticatable, :registerable, :recoverable, :rememberable
,就像身份验证\u用户代码>在设计中。在这个项目中,我有一个用户
模型,该模型由设计
和角色
生成,以设置用户角色。现在,我想在过滤器之前创建一个,
,检查用户对reach out controller的权限。以下代码显示模型:
user.rb
:
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_one :role
before_create :build_role
end
class Role < ActiveRecord::Base
belongs_to :user
end
private
def authorize_superadmin!
unless current_user.role.superadmin?
render :status => 411, :json => { :success => false, :info => "Not SuperAdmin" }
end
end
角色
有2个布尔列:超级管理员、管理员。我只想超级管理员有权联系一些管理员。为此,我在application\u controller.rb
中定义了以下函数:
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_one :role
before_create :build_role
end
class Role < ActiveRecord::Base
belongs_to :user
end
private
def authorize_superadmin!
unless current_user.role.superadmin?
render :status => 411, :json => { :success => false, :info => "Not SuperAdmin" }
end
end
但是当我在操作之前使用时:授权超级管理员代码>在控制器中,我在服务器日志中看到以下错误:
Completed 500 Internal Server Error in 25ms (Views: 0.0ms | ActiveRecord: 0.0ms)
我想我没有正确定义before_动作。我怎样才能解决这个问题?或者正确的定义方法是什么?您有什么错误?@mgh给我们错误,而不是HTTP返回码。@Geoffroy我发布了服务器日志,但我找不到错误。我不确定定义。我搜索了一下,但没有找到任何关于在\u action
@mgh之前创建个人的有用文档。您的语法是正确的,但您仍然没有给出实际错误,这将更有用!在开发中运行,而不是在需要时进行生产。您的代码中可能有一个标准错误。。。比如“未知方法superadmin?对于nil:NilClass”,或者其他什么。