【问题标题】:Rails Devise User Exists on empty DBRails 设计用户存在于空数据库中
【发布时间】:2013-04-25 00:06:30
【问题描述】:

我正在尝试通过 Devise 创建用户,但遇到了问题。

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。

这会在提交时出现:

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 console:

2.0.0p0 :011 > User.all
  User Load (0.5ms)  SELECT "users".* FROM "users"
 => #<ActiveRecord::Relation []> 

localhost:5000/users 完全是空的...

当我的数据库中没有用户时,为什么我会得到一个用户存在和回滚?

【问题讨论】:

    标签: ruby-on-rails devise rake


    【解决方案1】:

    用户存在查询来自验证器,检查是否没有用户与您尝试注册的电子邮件相同。永远不会有 INSERT 查询,因此您的用户永远不会被注册。

    这可能是验证失败。在这种情况下,您的密码可能太短(默认的 Devise 密码长度验证要求长度至少为 8,而您的只有 4)。

    您收到成功的 200 响应,因为它正在重定向回注册,并带有一条解释验证失败的消息。

    【讨论】:

    • yupi ... device.rb 初始化程序中的该死限制:P 非常感谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-08-26
    • 2011-01-14
    • 2011-12-10
    • 2013-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多