当仅发现1个错误时,Devise返回2个错误 [英] Devise returns 2 errors when only 1 should be found

查看:77
本文介绍了当仅发现1个错误时,Devise返回2个错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为我的应用启用了模块 validatable 。在屏幕上,当我询问用户的电子邮件地址进行注册时,如果该电子邮件已经存在...这是我看到的错误:

I have module validatable enabled for my app. On a screen when I am asking for the user's email address for registration, if the email already exists...this is the error I see:

We found 2 errors that prevented your account from being created:
Email has already been takenEmail has already been taken

这是从日志中获取的信息:

This is from the logs:

Started POST "/users" for 127.0.0.1 at 2012-06-21 14:37:41 -0500
Processing by RegistrationsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"zcpDH5U4XkHcYWVHEqrshgmzGiqsZHI9mD6Inrdr8uE=", "user"=>{"email"=>"abc@email.com"}, "commit"=>"Sign Me Up!"}
   (0.1ms)  begin transaction
  User Exists (0.2ms)  SELECT 1 AS one FROM "users" WHERE "users"."email" = 'abc@email.com' LIMIT 1
  User Exists (0.1ms)  SELECT 1 AS one FROM "users" WHERE LOWER("users"."email") = LOWER('abc@email.com') LIMIT 1
   (0.1ms)  rollback transaction
  Rendered devise/shared/_links.erb (0.8ms)
  Rendered devise/registrations/new.html.erb within layouts/application (6.5ms)
Completed 200 OK in 1118ms (Views: 290.9ms | ActiveRecord: 0.0ms)

我如何通过尝试两次检查和仅执行1次检查来得到它?

How do I get it from trying to do two checks and just do 1?

推荐答案

Rails为每个验证运行单独的检查,因此存在和唯一性验证器将导致两个检查。如果要避免出现多余的错误消息,可以执行以下操作,仅输出唯一的错误消息:

Rails runs separate checks for each validation, so a presence and a uniqueness validator will result in two checks. If you want to avoid the redundant error messages, you can do something like the below and only output unique error messages:

#error_explanation
  .alert.alert-error
    Please correct the following #{pluralize(object.errors.full_messages.uniq.size, "error")}:
   %ul
   - object.errors.full_messages.uniq.each do |msg|
     %li #{msg}

这篇关于当仅发现1个错误时,Devise返回2个错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆