9

我在我的 ruby​​ on rails 应用程序中使用 devise gem。在用户注册时,如果电子邮件已经存在,则默认消息“电子邮件已被使用”。

我已在 en.yml 中更改了此消息

  activerecord:
    errors:
      messages:
        taken: "User with same email already exists. Please try with another email address."

鉴于我使用过:

<div class="notice"><%= devise_error_messages! %></div>

现在我得到的信息是

"Email User with same email already exists. Please try with another email address."

问题是在开头附加了“电子邮件”。

有没有其他方法可以更改此默认消息?

4

1 回答 1

10

将消息格式更改为

en:
  errors:
    format: "%{message}"

默认为"%{attribute} %{message}"

更新

还有另一种解决方案。我知道这是一种解决方法,但是这里可以......删除现有的验证,并添加一个自定义的验证。

validate :email_uniqueness

def email_uniqueness
  self.errors.add(:base, 'User with same email already exists. Please try with another email address.') if User.where(:email => self.email).exists?
end

注意:您应该在进行更新时考虑现有用户

于 2013-07-10T12:24:22.803 回答