How to create the first (Admin) user (CanCan and Devise)?
Asked Answered
E

3

33

I made authentication in my Rails 3 app fallowed by Tony's tutorial

I don't want public registrations on my app, just to create new users with Admin account, but I can't create Admin account manually, because in table Users there is encrypted password and salt that must to be generated, and I don't know how :|

Etymologize answered 1/3, 2011 at 2:3 Comment(2)
What does this question have to do with CanCan?Jankowski
@Mark S...I dont know .?Scaremonger
F
45

You can do it from the rails console. From the command line goto the directory of your rails application and type rails console. Then enter the following code to create a user:

user=User.create!(:email=>'[email protected]',:username=>'test',:password=>'password')

This will create a user object (assuming your devise resource is called User). Now you can use the user object that you just created to set admin privileges.

Fairy answered 1/3, 2011 at 3:44 Comment(3)
I get Template error in browser when I want to access users/new, undefined method 'model', and from console when I try to create new user I get ActionView::Template::Error: ActionView::Template::Error and a lot of file references (it's not nice to copy in comment :) )Etymologize
Where is your ability model? - github.com/ryanb/cancan/wiki/defining-abilitiesFairy
The default devise user model does not have a :username field.Draughtsman
J
21

I am current something like this (your details may be different) in my seeds.rb file to create my admin user for Devise.

User.new({ :email => '[email protected]', :password => 'password', :password_confirmation => 'password'}).save

You can execute it using rake db:seed in the terminal window.

Jankowski answered 2/3, 2011 at 0:32 Comment(0)
H
18

In addition, if you are using confirmable and want to skip the requirement for a confirmation email when creating new accounts you can do something like this:

newuser = User.new({ :email => '[email protected]', 
          :password => 'password', 
          :password_confirmation => 'password'})
newuser.skip_confirmation!
newuser.save

This is useful if the accounts you are creating are for trusted users or if you are creating test accounts.

Hemidemisemiquaver answered 11/10, 2011 at 15:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.