Rails rails4.0.1 devise 创建用户问题

andy_zh_cn · 2014年03月31日 · 最后由 hz_qiuyuanxin 回复于 2014年03月31日 · 2920 次阅读

model

class User
     attr_accessable  :xrk_user_id,  :user_name, :status, :user_type,:encrypted_password, :password, :current_password
end

db/migrate :reate_table

 def change
    create_table :users, comment: "用户表" do |t|
      t.string   :user_name, null: false, comment: "用户姓名"
      t.integer  :user_type, null: false, comment: "用户类型  0:审核人 1:提单人"
      t.boolean  :status,    null: false, comment: "用户状态  false: 无 效 true: 有 效"
      t.integer  :xrk_user_id, null: false, comment: "用户ID"

      ## Recoverable
      t.string   :reset_password_token
      t.datetime :reset_password_sent_at
      t.string   :encrypted_password, :null => false, :default => ""
      ## Rememberable
      t.datetime :remember_created_at

      ## Trackable
      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

      ## Lockable
      t.integer  :failed_attempts, :default => 0, :null => false # Only if lock strategy is :failed_attempts
      t.string   :unlock_token # Only if unlock strategy is :email or :both
      t.datetime :locked_at

      t.timestamps
    end

console(rails c)
>> user_attr = {:xrk_user_id=>8, :user_name=>"dev", :user_type=>1, :password=>"111111", :status=>true}
=>  {:xrk_user_id=>8, :user_name=>"dev", :user_type=>1, :password=>"111111", :status=>true}
>> user = User.new user_attr
=> #<User id: nil, user_name: "dev", user_type: 1, status: true, xrk_user_id: 8, reset_password_token: nil, reset_password_sent_at: nil, encrypted_password: "$2a$10$c8fJb8GKGMg.YmwWcCT86uuImVwQ.JTtZ/8RXIM90T4k...", 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, failed_attempts: 0, unlock_token: nil, locked_at: nil, created_at: nil, updated_at: nil>

>> user.save
INSERT INTO "users" ("created_at", "encrypted_password", "status", "updated_at", "user_name", "user_type") VALUES ($1, $2, $3, $4, $5, $6) RETURNING "id"  [["created_at", Mon, 31 Mar 2014 09:38:59 CST +08:00], ["encrypted_password", "$2a$10$c8fJb8GKGMg.YmwWcCT86uuImVwQ.JTtZ/8RXIM90T4kz7XB01SEa"], ["status", true], ["updated_at", Mon, 31 Mar 2014 09:38:59 CST +08:00], ["user_name", "dev"], ["user_type", 1]]
PG::NotNullViolation: ERROR:  null value in column "xrk_user_id" violates not-null constraint
: INSERT INTO "users" ("created_at", "encrypted_password", "status", "updated_at", "user_name", "user_type") VALUES ($1, $2, $3, $4, $5, $6) RETURNING "id"
   (0.2ms)  ROLLBACK
ActiveRecord::StatementInvalid: PG::NotNullViolation: ERROR:  null value in column "xrk_user_id" violates not-null constraint
: INSERT INTO "users" ("created_at", "encrypted_password", "status", "updated_at", "user_name", "user_type") VALUES ($1, $2, $3, $4, $5, $6) RETURNING "id"

在 new 一个 user 的时候 xrk_user_id 时有值,但是 save 的时候却被清空了。请问这个是什么问题啊????,在线等啊....(数据库用的是 PG)

自己给自己顶一个吧...

首先,生成的 SQL 语句就已经有问题了,并没有出现 xrk_user_id字段; 其次,我记得建一条记录的时候,密码必须同时填写:passwordpassword_confirmation。 如:

User.new login: "test", password: "123456", password_confirmation: "123456"
需要 登录 后方可回复, 如果你还没有账号请 注册新账号