如何让第一个用户成为Devise宝石的管理员? [英] How to make first user became admin in Devise gem?
问题描述
我想让第一个用户成为管理员。
所以我有字符串属性管理员,我需要做一些过滤或覆盖设计注册控制器。如下所示:
I want to make first user became admin.
So I have string attribute admin and I need to make some before filter or override Devise registration controller. Something like:
if User.first?
User.admin = "admin"
User.save
end
这里是设计注册动作
def new
resource = build_resource({})
respond_with resource
end
这是更好的方法?
推荐答案
假设你的代码不是很经常运行(只有在第一次运行你的应用程序的时候),我只会写一个这样做的Rake任务对于你一次,而不是杂乱的你的应用程序代码。
Assuming your code is not running very often (only when running your app in production for the first time) I would simply write a Rake task that does this for you once instead of cluttering your application code.
把它放在你的 Rakefile
task :promote_admin => :environment do
User.first.update_attribute('admin', 'admin')
end
这样,您可以保持代码清洁,同时在安装过程中仍然有一个非常简单的方法来宣传第一个用户。
This way you keep your code clean while still having a very easy way to promote the first user during installation.
如果要宣传第一个用户您只需从控制台运行 rake promotion_admin
,第一个用户将被升级为admin。
If you want to promote the first user you simply run rake promote_admin
from the console and the first User will be promoted to admin.
这篇关于如何让第一个用户成为Devise宝石的管理员?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!