【问题标题】:Change devise message "Email has already been taken"更改设计消息“电子邮件已被占用”
【发布时间】:2013-07-10 12:06:12
【问题描述】:

我在我的 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."

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

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

【问题讨论】:

标签: ruby-on-rails devise


【解决方案1】:

将消息格式更改为

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

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

【讨论】:

  • 感谢您的回复。它的工作。我可以只更改此消息的格式吗??
  • AFIK,你不能。您必须插入所有其他消息以包含属性名称。或者,您可以对错误消息进行一些正则表达式替换,而不是更改默认格式。
  • @Arif:如果它有效,你为什么不接受答案?
猜你喜欢
  • 2012-02-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-04
  • 2015-10-24
  • 1970-01-01
相关资源
最近更新 更多