使用 Rails 3.2 和 Devise,我用自定义控制器覆盖了注册控制器。方法里的代码我完全没有改create
,是Devise中的原版。
但奇怪的是,每当我尝试创建/注册新用户时,我都会收到此错误。
PG::Error: ERROR: null value in column "email" violates not-null constraint
: INSERT INTO "users" ("created_at", "current_sign_in_at", "current_sign_in_ip", "email", "last_sign_in_at", "last_sign_in_ip", "payroll", "sign_in_count", "updated_at", "username") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10) RETURNING "id"
但在它下面,一切似乎都很好,因为用户正在收到一封有效的电子邮件。
{"utf8"=>"✓",
"authenticity_token"=>"8/ZKW11lF22WYKV2K36zBkSk6DSU36/1/zU54a2IRmM=",
"user"=>{"username"=>"someguy",
"email"=>"email@yahoo.com",
"password"=>"[FILTERED]",
"password_confirmation"=>"[FILTERED]"},
"commit"=>"Sign up",
"format"=>"user"}
我不太确定要在此处粘贴哪些其他代码,因为一切显然都适合我。所以请询问任何可能有帮助的东西。
这是我的注册控制器,但create
基本上具有相同的代码super
。错误发生在具有resource.save
.
class UserRegistrationsController < Devise::RegistrationsController
def new
super
end
def create
build_resource
if resource.save
if resource.active_for_authentication?
set_flash_message :notice, :signed_up if is_navigational_format?
sign_up(resource_name, resource)
respond_with resource, :location => after_sign_up_path_for(resource)
else
set_flash_message :notice, :"signed_up_but_#{resource.inactive_message}" if is_navigational_format?
expire_session_data_after_sign_in!
respond_with resource, :location => after_inactive_sign_up_path_for(resource)
end
else
clean_up_passwords resource
respond_with resource
end
end
def update
super
end
end
显然,所有字段都是nil
. 这是它执行的查询:
INSERT INTO "users" ("created_at", "current_sign_in_at", "current_sign_in_ip", "email", "first_name", "last_name", "last_sign_in_at", "last_sign_in_ip", "payroll", "sign_in_count", "updated_at", "username") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12) RETURNING "id" [["created_at", Wed, 17 Apr 2013 19:34:22 BST +01:00], ["current_sign_in_at", nil], ["current_sign_in_ip", nil], ["email", nil], ["first_name", nil], ["last_name", nil], ["last_sign_in_at", nil], ["last_sign_in_ip", nil], ["payroll", nil], ["sign_in_count", 0], ["updated_at", Wed, 17 Apr 2013 19:34:22 BST +01:00], ["username", ""]]