Creating an admin user in Devise on Rails beta 3

前端 未结 6 1204
梦谈多话
梦谈多话 2020-12-22 16:35

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

6条回答
  •  囚心锁ツ
    2020-12-22 17:21

    There is convenient way for populating tables - db/seed.rb file. Just add the script for creating users in it and run:

    rake db:seed
    

    Below you can see example of User model with email and username fields:

    # Inserting default security users
    users = {
    
        admin: {
    
            username: 'admin',
            email: 'admin@gmail.com',
            password: 'adminpass',
            password_confirmation: 'adminpass',
            is_admin: true
        },
    
        administrator: {
    
            username: 'administrator',
            email: 'administrator@gmail.com',
            password: 'administrator',
            password_confirmation: 'administrator',
            is_admin: true
        }
    }
    
    users.each do |user, data|
    
      user = User.new(data)
    
      unless User.where(email: user.email).exists?
        user.save!
      end
    end
    

    Note, that devise validations are applied here.

    Here you can find more examples of using the seed.rb file and here is the rayn's rails cast.

提交回复
热议问题