我正在尝试使用Rails中的Devise设置两步注册过程,并遵循此tutorial by Claudio Marai。
当我在表单中输入电子邮件地址时(第一步),我收到一条错误消息,告诉我路由错误并且找不到RegistrationsController
Started POST "/users" for 127.0.0.1 at 2012-05-03 22:50:59 -0400
ActionController::RoutingError (uninitialized constant RegistrationsController):
activesupport (3.2.1) lib/active_support/inflector/methods.rb:229:in `block in constantize'
activesupport (3.2.1) lib/active_support/inflector/methods.rb:228:in `each'
activesupport (3.2.1) lib/active_support/inflector/methods.rb:228:in `constantize'
.
.
.
我认为这是由于我:registrations => "registrations"
中存在routes.rb file
,如the tutorial的第一步所述。然后我尝试了两个替代方案,这两个方案都导致了同样的错首先,我从:registrations => "registrations"
中删除了routes.rb file
。当这不起作用时,我重新插入该行并在registrations_controller.rb
目录中添加controllers
,如下所示:
class RegistrationsController < Devise::RegistrationsController
end
我认为这两个选项会产生相同的效果 - 但是 - 无论如何我都试过了。
我得到的错误如下:
Started POST "/users" for 127.0.0.1 at 2012-05-03 22:47:29 -0400
Processing by Devise::RegistrationsController#create as HTML
Parameters: {"utf8"=>"?", "authenticity_token"=>"ttPBRPRLVzPBHcDDKRJbimv0Yp/egdK5qBkIvBTL4Ig=", "user"=>{"email"=>"test@email.com"}, "x"=>"0", "y"=>"0"}
(0.6ms) begin transaction
User Exists (1.5ms) SELECT 1 FROM "users" WHERE "users"."email" = 'test@email.com' LIMIT 1
CACHE (0.0ms) SELECT 1 FROM "users" WHERE "users"."email" = 'test@email.com' LIMIT 1
User Load (0.6ms) SELECT "users".* FROM "users" WHERE "users"."confirmation_token" = '8tA1jakpAXNK4Piz7J6R' LIMIT 1
SQL (14.0ms) INSERT INTO "users" ("confirmation_sent_at", "confirmation_token", "confirmed_at", "created_at", "current_sign_in_at", "current_sign_in_ip", "email", "encrypted_password", "fb_id", "fb_token", "first_name", "last_name", "last_sign_in_at", "last_sign_in_ip", "remember_created_at", "reset_password_sent_at", "reset_password_token", "sign_in_count", "state", "updated_at") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) [["confirmation_sent_at", Thu, 03 May 2012 22:47:30 EDT -04:00], ["confirmation_token", "8tA1jakpAXNK4Piz7J6R"], ["confirmed_at", nil], ["created_at", Thu, 03 May 2012 22:47:30 EDT -04:00], ["current_sign_in_at", nil], ["current_sign_in_ip", nil], ["email", "test@email.com"], ["encrypted_password", nil], ["fb_id", nil], ["fb_token", nil], ["first_name", nil], ["last_name", nil], ["last_sign_in_at", nil], ["last_sign_in_ip", nil], ["remember_created_at", nil], ["reset_password_sent_at", nil], ["reset_password_token", nil], ["sign_in_count", 0], ["state", nil], ["updated_at", Thu, 03 May 2012 22:47:30 EDT -04:00]]
SQLite3::ConstraintException: constraint failed: INSERT INTO "users" ("confirmation_sent_at", "confirmation_token", "confirmed_at", "created_at", "current_sign_in_at", "current_sign_in_ip", "email", "encrypted_password", "fb_id", "fb_token", "first_name", "last_name", "last_sign_in_at", "last_sign_in_ip", "remember_created_at", "reset_password_sent_at", "reset_password_token", "sign_in_count", "state", "updated_at") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
(0.3ms) rollback transaction
Completed 500 Internal Server Error in 441ms
ActiveRecord::StatementInvalid (SQLite3::ConstraintException: constraint failed: INSERT INTO "users" ("confirmation_sent_at", "confirmation_token", "confirmed_at", "created_at", "current_sign_in_at", "current_sign_in_ip", "email", "encrypted_password", "fb_id", "fb_token", "first_name", "last_name", "last_sign_in_at", "last_sign_in_ip", "remember_created_at", "reset_password_sent_at", "reset_password_token", "sign_in_count", "state", "updated_at") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)):
sqlite3 (1.3.5) lib/sqlite3/statement.rb:108:in `step'
sqlite3 (1.3.5) lib/sqlite3/statement.rb:108:in `block in each'
sqlite3 (1.3.5) lib/sqlite3/statement.rb:107:in `loop'
sqlite3 (1.3.5) lib/sqlite3/statement.rb:107:in `each'
.
.
.
以上错误让我很困惑!
使用相同的表单,当我添加password
和password_confirmation
字段时,事情就会顺利进行 - 创建用户帐户并使用确认链接向用户发送确认电子邮件。< / p>
对此我感谢任何帮助 - 谢谢!我正在使用Ruby 1.9.3-p125
和Rails 3.2.1
答案 0 :(得分:3)
问题不在于Rails验证,而是由于违反了数据库约束。
很可能,如果您的用户表迁移是由Devise生成的,那么架构包括:
"encrypted_password" varchar(128) DEFAULT '' NOT NULL
导致ConstraintException的SQL包括这个(省略了可读性):
SQL (14.0ms) INSERT INTO "users" (..., "encrypted_password", ...) VALUES (..., ?, ...) [..., ["encrypted_password", nil], ...]
因此,有些事情告诉您的数据库适配器设置encrypted_password = nil
。
你应该能够绕过它,或者运行一个删除encrypted_password
上的NOT NULL约束的迁移,或者像你一样做,传入encrypted_password
,但只是确保它在第一步中是空的,但在第二步中出现。