【问题标题】:Rails 4 Devise Seed Users with random passwordsRails 4 使用随机密码设计种子用户
【发布时间】:2015-05-01 10:13:31
【问题描述】:

我已在现有应用中安装了 Devise,并且需要为数据库设置 100 个用户。他们现在有了 Devise 添加的字段,例如电子邮件和 encrypted_pa​​ssword 等。我正在尝试的种子文件部分是这样的: (种子.rb...)

    users = [] # Empty array to store users
    100.times do
        username = "#{Faker::Vehicle.make}#{Faker::BaconIpsum.word}-#{rand(999)}"
        user_password = SecureRandom.base64(12)
        u = User.new
            u.id = SecureRandom.random_number(9999999999)
            u.username = username.gsub(/\s+/,"")
            u.password = user_password
            u.password_confirmation = user_password
            u.email = "#{Faker::Internet.free_email}"
            u.sign_in_count = 0
       u.save
       users << u # Put newly created user in the array
    end

只是用 NOT NULL 字段播种。当我尝试播种时,我收到以下错误:

ActiveRecord::StatementInvalid: Mysql2::Error: 字段 'password' 没有默认值: INSERT INTO users (email, encrypted_password, id, username) VALUES (' colton@gmail.com', '$2a$10$F9RR6h3R48ySJ3./lrPgfeqE84MgPJh7TVseXL.ngwU1Xp5hYxPDC', 3494734525, 'Mitsubhibrisket-872')

编辑:用户表的 schema.rb,由迁移生成...

    create_table "users", force: true do |t|
        t.string   "username",               limit: 30,              null: false
        t.string   "password",               limit: 30,              null: false
        t.string   "email",                             default: "", null: false
        t.string   "encrypted_password",                default: "", null: false
        t.string   "reset_password_token"
        t.datetime "reset_password_sent_at"
        t.datetime "remember_created_at"
        t.integer  "sign_in_count",                     default: 0,  null: false
        t.datetime "current_sign_in_at"
        t.datetime "last_sign_in_at"
        t.string   "current_sign_in_ip"
        t.string   "last_sign_in_ip"
     end

     add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true, using: :btree
      add_index "users", ["username", "id"], name: "BY_USERNAME", using: :btree

显然,我在这里传递了“密码”的值。在 SO 等其他帖子之后,我学会了发送“密码”和“密码确认”,而不是尝试发送“加密密码”。仍然得到同样的错误。为什么这不起作用? 我应该在我的seeds.rb 文件中要求“设计”吗? 阅读错误,似乎我应该更改“密码”字段以允许 NULL 并只存储“加密”密码。

请帮忙,谢谢!

【问题讨论】:

  • “users”表的 schema.rb 是什么样子的?
  • @dgilperez 更新了我的帖子以显示用户表的 schema.rb

标签: mysql authentication ruby-on-rails-4 devise seeding


【解决方案1】:

我修好了。我所做的是删除数据库并进入创建用户表的迁移文件。 不是“add_devise_to_users”迁移,而是原始迁移。这是密码字段所在的位置。更改为添加默认值。现在看起来是这样的……

    class CreateUsers < ActiveRecord::Migration
      def self.up
        # create USERS table
          create_table "users", id: false, force: true do |t|
            t.integer "id",       limit: 8,  default: 0,  null: false
            t.string  "username", limit: 30, default: "", null: false
            t.string  "password", limit: 30, default: "", null: false   
          end
          execute "ALTER TABLE users ADD PRIMARY KEY (id);"
          add_index "users", ["username", "id"], name: "BY_USERNAME", using: :btree
      end

      def self.down
        drop_table :users
      end
    end

如您所见,我还为“id”字段添加了一个默认值,因为显然 Devise 对此存在问题。更改此设置后,运行 rake db:create、rake db:migrate 和 rake db:seed。

这解决了我的问题,我可以使用seeds.rb 创建用户和种子。起初,它看起来像是一件不安全的事情——为密码和用户 ID 提供默认值。但是在它被验证为“唯一性”的地方,我想没关系。

欢迎评论

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-10-21
    • 2021-11-07
    • 2020-04-07
    • 1970-01-01
    • 2017-10-13
    • 1970-01-01
    • 2013-05-06
    • 1970-01-01
    相关资源
    最近更新 更多