Rails:使用devise可以查看所有用户添加管理角色 [英] Rails : Adding an admin role using devise who can see all the users

查看:138
本文介绍了Rails:使用devise可以查看所有用户添加管理角色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用devise为我的应用程序创建一个管理角色。我使用devise创建了基本身份验证。我的应用程序中有一个设计用户模型,但现在我需要一个管理员可以显示编辑和销毁所有的用户。我尝试遵循教程,但没有一个帮助。
我正在使用rails 3.0.10和ruby 1.9.2-p290。

解决方案

首先通过创建migratioin来定义role.rb

  rails g model角色名称:string 

然后在role.rb

  class Role 
has_one:user
end
/ pre>

在用户模型中

  class user 
belongs_to:角色
end

将两个角色插入DB

  1.admin 
2.user

然后检查这个

 如果user.role.name ==admin
#可以做所有的逻辑
else
你的逻辑
end

确保将role_id:integer插入用户模型



尝试.......


I need to create an admin role using devise for my app. I've created basic authentication using devise . I have a devise user model in my app but now i need an admin who can show edit and destroy all the users. I tried following the tutorials but none of them helped. I am using rails 3.0.10 and ruby 1.9.2-p290.

解决方案

You just define role.rb first by creating migratioin

      rails g model role  name:string

then in role.rb

      class Role
         has_one:user
      end

And in user model

      class user
         belongs_to :role
      end

Insert two roles into DB

   1.admin
   2.user

Then check by this

    if user.role.name == "admin"  
        # can do all your logic
    else
         your logic
    end

Make sure insert role_id:integer into user model

Try it.......

这篇关于Rails:使用devise可以查看所有用户添加管理角色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆