prepopulating admin user in database with authlogic rails plugin

你。 提交于 2019-12-05 18:33:40

Rails 2.3.4 adds a new feature to seed databases.

You can add in your seed in db/seed.rb file:

User.create(:username => "admin", :password => "notthis", :password_confirmation => "notthis", :email => "admin@example.com")

Then insert it with:

rake db:seed

for production or test

RAILS_ENV="production" rake db:seed  
RAILS_ENV="test" rake db:seed

My favorite feature in 2.3.4 so far

If you are using >= Rails 2.3.4 the new features include a db/seeds.rb file. This is now the default file for seeding data.

In there you can simple use your models like User.create(:login=>"admin", :etc => :etc) to create your data.

With this approach rake db:setup will also seed the data as will rake db:seed if you already have the DB.

In older projects I've sometimes used a fixture (remeber to change the password straight away) with something like users.yml:

admin:
  id: 1
  email: admin@domain.com
  login: admin
  crypted_password: a4a4e4809f0a285e76bb6b35f97c9323e912adca
  salt: 7e8455432de1ab5f3fE0e724b1e71500a29ab5ca
  created_at: <%= Time.now.to_s :db %>
  updated_at: <%= Time.now.to_s :db %>

rake db:fixtures:load FIXTURES=users

Or finally as the other guys have said you have the rake task option, hope that helps.

Most used approach is to have a rake task that is run after deployment to host with empty database.

Add a rake task:

  # Add whatever fields you validate in user model
  # for me only username and password
  desc  'Add Admin: rake add_admin username=some_admin password=some_pass'
  task :add_admin => :environment do
    User.create!(:username=> ENV["username"], :password=> ENV["password"],:password_confirmation => ENV["password"])
  end
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!