gpt4 book ai didi

ruby-on-rails - 更改设计消息 "Email has already been taken"

转载 作者:行者123 更新时间:2023-12-04 07:23:57 25 4
gpt4 key购买 nike

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

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

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

最佳答案

将消息的格式更改为

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

笔记:
在进行更新时,您应该考虑现有用户

关于ruby-on-rails - 更改设计消息 "Email has already been taken",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17570239/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com