Jerry Lowery
Jerry Lowery

Reputation: 1

How do you add an administratior in a rails 3 appliction

I have just built a rails 3 application by using Mike Hartl's "Learn Rails by Example". I am ready to deploy it but I am confused about how to add the administrator to the application. I will be the only administrator. Will the administrator be added before deployment and if so how do I do this.

Upvotes: 0

Views: 150

Answers (4)

Erin Walker
Erin Walker

Reputation: 739

I'm busy with the same thing!

I found this on STACK:

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

I changed the Admin to User, but the problem is it creates a normal user not an admin user. Somewhere we need to put in - admin.toggle!(:admin) or make it true. I'll let you know if I find anything else.

Upvotes: 0

Agung Prasetyo
Agung Prasetyo

Reputation: 4483

for admin control panel on my web-apps i'm using typus gem https://github.com/fesplugas/typus/

it will generate an admin page and by the default typus will use your model default_scope to fetch data.

Upvotes: 0

Danny
Danny

Reputation: 1

The tutorial doesn't actually go through creating an interface where you can create admins. If you want to test the part where you're not allowed to delete other administrator accounts, you can test it with faker by adding 2 admins to the sample_data.rake file:

def make_users
  admin = User.create!(:name => "Example User",
                   :email => "[email protected]",
                   :password => "foobar",
                   :password_confirmation => "foobar")
admin.toggle!(:admin)
admin2 = User.create!(:name => "Example User2",
                   :email => "[email protected]",
                   :password => "foobar",
                   :password_confirmation => "foobar")
admin.toggle!(:admin)
99.times do |n|
name  = Faker::Name.name
email = "example-#{n+1}@railstutorial.org"
password  = "password"
User.create!(:name => name,
             :email => email,
             :password => password,
             :password_confirmation => password)
  end
end

If you want to add an admin to production, I'm guessing you could create your account and toggle the admin function with a database editor and then push the db to the production server? That's what I would do but I'm by no means an expert.

Upvotes: 0

plang
plang

Reputation: 5646

What I believe you need when you talk about an "administrator account" is in fact two different things: authentication (the login) & authorization (what a login can/cannot do).

Under rails, one way to do that is by using two different gems. I suggest you have a look at devise, and cancan. They have both been developed and are actively maintained by rails superstars: José Valim and Ryan Bates.

Upvotes: 2

Related Questions