Add an ActiveAdmin AdminUser Through Rails Console

Why this post?

I was working on an app and wanted to wipe out the database. So I did this:

$ rake db:reset

This wipes out all data from your database and loads the schema back. No need to migrate anything again.

Doing this deleted the default AdminUser that ActiveAdmin adds when installing the gem.

I lost access to Active Admin because I removed the default AdminUser and I cannot login.

Add an AdminUser through the rails console

Go to the app and enter the rails console

$ rails c

Then you could do this:

> u=AdminUser.new(:email => "admin@example.com", :password => 'password', :password_confirmation => 'password')
> u.save

It will say something like:

(0.2ms)  SELECT 1 FROM "admin_users" WHERE "admin_users"."email" = 'admin@example.com' LIMIT 1

SQL (5.9ms)  INSERT INTO "admin_users" ("created_at", "current_sign_in_at", "current_sign_in_ip", "email", "encrypted_password", "last_sign_in_at", "last_sign_in_ip", "remember_created_at", "reset_password_sent_at", "reset_password_token", "sign_in_count", "updated_at") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)  [["created_at", Thu, 21 Jun 2012 23:51:11 UTC +00:00], ["current_sign_in_at", nil], ["current_sign_in_ip", nil], ["email", "admin@example.com"], ["encrypted_password", "some weird number here"], ["last_sign_in_at", nil], ["last_sign_in_ip", nil], ["remember_created_at", nil], ["reset_password_sent_at", nil], ["reset_password_token", nil], ["sign_in_count", 0], ["updated_at", Thu, 21 Jun 2012 23:51:11 UTC +00:00]]
 => true

If you get a: => false

On the same object you created. My object was called: u

> u.errors

It could say something like:

=> #<ActiveModel::Errors:0x007ff100ddc068 @base=#<AdminUser id: nil, email: "admin@example.com", encrypted_password: "", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 0, current_sign_in_at: nil, last_sign_in_at: nil, current_sign_in_ip: nil, last_sign_in_ip: nil, created_at: nil, updated_at: nil>, @messages={:password=>["can't be blank"]}

The previous error says that I didn’t add a password.

How to enter a password for AdminUser. The schema only shows ‘encrypted_password’

This the way. See again the example:

> u=AdminUser.new(:email => "admin@example.com", :password => 'password', :password_confirmation => 'password')

You have to use these two parameters:

:password
:password_confirmation

Login to localhost:3000/admin

Now you can enter the user and password you just created through the rails console.