views:

2265

answers:

5

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: http://wiki.github.com/plataformatec/devise/adding-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!

A: 

try appending /sign_in to your admin path, whatever you set it to...mine is

http://yoursite.com/admin/sign_in?unauthenticated=true

kinet
oops you likely didn't use :authenticable. Try this page for help on overriding the admin views: http://wiki.github.com/fortuity/subdomain-authentication/tutorial-walkthrough
kinet
No, I didn't use :authenticatable when setting up the admin model with devise, like I said, I was using the tutorial that divise has on their github page. I would prefer not to use subdomains if I can help it...
erskingardner
+8  A: 

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.

erskingardner
The vote up was not referring to the first sentence of your answer.
Luca
A: 

What I dont understand is why devise needs to have 2 models for admin and user. Why not just have a flag in the user table?

Stewart
It really depends on the application. Maybe Admins are completely different than users. Then you can use a polymorphic "Authenticatable" class for the login stuff so you keep it DRY
Tony
A: 

Try taking a look at CanCan to provide roles. It's compatible with Devise.

http://www.tonyamoyal.com/2010/07/28/rails-authentication-with-devise-and-cancan-customizing-devise-controllers/

Thanks for the shout out. Also wrote a new article on adding a RESTful users controller if you want your "Admin" to be able to CRUD "Users" - http://www.tonyamoyal.com/2010/09/29/rails-authentication-with-devise-and-cancan-part-2-restful-resources-for-administrators/
Tony
A: 

I put my first admin user in my seeds so it gets automatically created when bootstrapping my app. I also use a RESTful interface for managing my users as an Admin and I put the code up here - http://www.tonyamoyal.com/2010/09/29/rails-authentication-with-devise-and-cancan-part-2-restful-resources-for-administrators/

Tony