Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating an admin user in Devise on Rails beta 3

Ok, I'm probably going to feel quite dumb when someone answers this one with a simple thing that I'm missing but... here goes:

I've got a brand new app on rails 3 beta and I'm using devise for the authentication. I've run all the comments and everything is working perfectly at the moment. I've created a user role and an admin role (following these instructions: https://github.com/plataformatec/devise/wiki/How-To:-Add-an-Admin-Role) and I've registered myself as the first user but how to do I register or create an admin role user? The directions from the devise guys setup the admin role to not be registerable but I'm unsure how you're supposed to create the admin if you can't register?!

Any help would be appreciated! Thanks!

like image 774
erskingardner Avatar asked Apr 25 '10 13:04

erskingardner


2 Answers

Yup. I feel dumb.

If anyone else is having a similarly vapid moment. Just use the rails console to create the admin user:

➡ rails c Loading development environment (Rails 3.0.0.beta3) irb(main):001:0> admin = Admin.create! do |u| irb(main):002:1* u.email = '[email protected]' irb(main):003:1> u.password = 'password' irb(main):004:1> u.password_confirmation = 'password' irb(main):005:1> end 

That will do it. Now just visit your admin sign in path and sign in.

like image 129
erskingardner Avatar answered Sep 30 '22 17:09

erskingardner


What you are really trying to do is create seed data. A more standard way to do this would be to add your seed users (and roles, if you are storing them) to db/seeds.rb

For exmaple in db/seeds.rb:

roles = Role.create([{name: 'super_admin'}, {name: 'staff'}, {name:'customer'}]) users = User.create([{email: '[email protected]', first_name: 'super', last_name: 'admin', password: '@dmin123', password_confirmation: '@dmin123', role: roles[0]}]) 

Then run:

rake db:seed 
like image 37
paneer_tikka Avatar answered Sep 30 '22 18:09

paneer_tikka