设计validates_uniqueness无声地失败

时间:2012-06-21 09:05:49

标签: ruby-on-rails-3 devise

这是我的用户模型:

class User < ActiveRecord::Base
  devise :database_authenticatable, :registerable, :token_authenticatable, :confirmable, :timeoutable, 
         :recoverable, :rememberable, :trackable, :validatable,
                 :email_regexp =>  /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i

  attr_accessible :email, :password, :password_confirmation, :remember_me, :name, :confirmed_at, :confirmation_token, :category_ids
    validates_uniqueness_of :email, :case_sensitive => false
    validates_confirmation_of   :password

end

当我输入数据库中存在的电子邮件地址时,我的页面无法重新加载,我看不到任何错误消息或任何内容。

这就是我在日志中看到的内容:

Started GET "/validators/uniqueness?case_sensitive=false&user%5Bemail%5D=abc%40email.com" for 127.0.0.1 at 2012-06-21 03:59:57 -0500
  User Exists (0.2ms)  SELECT 1 AS one FROM "users" WHERE ("users"."email" LIKE 'abc@email.com') LIMIT 1


Started GET "/validators/uniqueness?case_sensitive=false&user%5Bemail%5D=abc%40email.com" for 127.0.0.1 at 2012-06-21 03:59:57 -0500
  User Exists (0.2ms)  SELECT 1 AS one FROM "users" WHERE ("users"."email" LIKE 'abc@email.com') LIMIT 1

2 个答案:

答案 0 :(得分:0)

您为什么要覆盖设计验证?设计将处理一切。 日志表明你正在尝试进行某种JS表单验证,对吧?如果是这样,请尝试以不同方式检查现有用户:

email = params[:email] || ""
@exists = User.where(:email=>email.downcase).count

答案 1 :(得分:0)

  

“解决方案是执行完全自定义的验证,或者   像在示例中那样覆盖电子邮件验证

https://github.com/bcardarella/client_side_validations/issues/455#issuecomment-12170527