Rails Devise用户存在于空数据库中 [英] Rails Devise User Exists on empty DB
问题描述
我试图通过Devise创建一个用户,但是遇到了问题。
I am trying to create a User through Devise but am running into issues.
home#land
def land
@resource = User.new
@devise_mapping = Devise.mappings[:user]
end
land.html.haml
= form_for @resource, :as => :user, :url => registration_path(:user), :remote => true do
= label_tag 'user[email]', raw("<h3>Stay Informed!</h3>")
= text_field_tag 'user[email]', nil, {:placeholder => "Your email", :required => true}
%input(type="submit" name="commit" value="Add")
user.rb
class User < ActiveRecord::Base
before_validation(:on => :create) do
self.password = "none"
self.password_confirmation = "none"
end
# Include default devise modules. Others available are:
# :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me
end
正在使用默认Devise :: RegistrationsController。
Default Devise::RegistrationsController is being used.
出现在提交时:
development.log
Started POST "/users" for 127.0.0.1 at 2013-04-24 20:49:46 -0300
Processing by Devise::RegistrationsController#create as JS
Parameters: {"utf8"=>"✓", "user"=>{"email"=>"me@mjohnst.com"}, "commit"=>"Add"}
[1m[35m (0.3ms)[0m BEGIN
[1m[36mUser Exists (0.5ms)[0m [1mSELECT 1 AS one FROM "users" WHERE "users"."email" = 'me@mjohnst.com' LIMIT 1[0m
[1m[35m (0.2ms)[0m ROLLBACK
Rendered devise/registrations/create.js (0.4ms)
Completed 200 OK in 84ms (Views: 4.1ms | ActiveRecord: 1.0ms)
但是,在 rails控制台中 c>:
2.0.0p0 :011 > User.all
User Load (0.5ms) SELECT "users".* FROM "users"
=> #<ActiveRecord::Relation []>
和 localhost:5000 / users
完全是空...
当数据库中没有用户时,为什么我会存在用户并回滚?
Why am I getting a User Exists and ROLLBACK when there are no users in my DB?
推荐答案
用户存在查询来自验证器,检查是否存在与您尝试注册的电子邮件相同的用户。永远不会有INSERT查询,因此您的用户永远不会被注册。
The user exists query is from the validator checking that there isn't a user with the same email you are trying to register. There is never an INSERT query, so your user is never being registered.
这可能是验证失败。在这种情况下,您的密码可能太短了(默认的Devise密码长度验证至少需要8位密码,而您的密码只有4位)。
It is probably a validation failing. In this case your password may be too short (the default Devise password length validation requires at least a length of 8, yours is only 4).
您将收到一个成功的密码。 200响应,因为它通过一条简短的消息重定向回注册,解释了验证失败的原因。
You are receiving a successful 200 response because it is redirecting back to the registration with a flash message explaining what validations failed.
这篇关于Rails Devise用户存在于空数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!