简体   繁体   English

按路线设置设计角色

[英]Set Devise Role By Route

My app has 3 roles for users: member, handicapper, admin 我的应用程序对用户具有3个角色:成员,障碍者,管理员

I'm trying to set the role by the route, example: 我正在尝试通过路线设置角色,例如:

get '/members/sign_up' => 'devise/registrations#create', :role => 'member'

Then in my controller, which I use to override devise, I do something like this: 然后在我用来覆盖设计的控制器中,执行以下操作:

def create
  super do
    resource.role = params[:role]
    resource.save
  end
end

Please give some direction as to what I'm doing wrong, or what I else I need to be doing. 请就我做错了什么或我需要做的其他事情给出一些指导。

Also, I currently don't have a way of checking whether that parameter is even being passed to the action, so if you could suggest ways to do that, it would be greatly appreciated. 另外,我目前还没有办法检查该参数是否甚至传递给操作,因此,如果您可以提出建议的方法,将不胜感激。

Note: I have gone through several tutorials and documentation pages, as well as dozens of SO questions, but have yet to find anything this specific. 注意:我已经浏览了一些教程和文档页面,以及数十个SO问题,但是还没有找到任何具体的内容。

Thanks in advance. 提前致谢。

EDIT: I should add I have already added the field to the migration file, and ran rake db:migrate. 编辑:我应该添加我已经将该字段添加到迁移文件,并运行rake db:migrate。 EDIT 2: User.last in rails console shows role: nil 编辑2:在Rails控制台中的User.last显示role: nil

For the role column in the users table I would suggest using enum. 对于用户表中的角色列,我建议使用枚举。

add_column :users, :role, :integer, default: 0

Then in your user model add: 然后在您的用户模型中添加:

enum role: {member: 0, handicapper:1, admin: 2}

Now by default a user will always have a role of member. 现在,默认情况下,用户将始终具有成员角色。 Don't set a users role via a route - that sounds like a security hole. 不要通过路由设置用户角色-听起来像是安全漏洞。

If your letting users self register, let them do that and they will be automatically set to member, and then in an admin area you can set their role to something else if need be. 如果您让用户自行注册,让他们自己注册,他们会自动设置为成员,然后在管理区域中,您可以根据需要将其角色设置为其他角色。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM