gpt4 book ai didi

ruby-on-rails - validates_presence_of + :message shows the name of the field

转载 作者:行者123 更新时间:2023-12-03 00:53:46 25 4
gpt4 key购买 nike

我正在创建一个 Rails 应用程序,并且有一个名为 User 的模型。在此模型中,我有一个名为 isagirl 的 bool 值。用户必须指定它是否是女孩,这是通过两个单选按钮完成的。在我的模型中我有这个:

validates_presence_of :isagirl, :message => "You must be either a Boy or a Girl. If not, please contact us."

但是,当我不指定性别时,我会看到以下内容:

Isagirl You must be either a Boy or a Girl.

作为错误消息。问题是“Isagirl”不能出现在错误消息中。我怎样才能禁用它?不,使用 CSS 来隐藏它是没有选择的。

谢谢

最佳答案

我这样做的方法是输出不带字段名称的消息。例如,我有一个在验证失败后输出错误消息的部分。

<ul>
<% errors.each do |attribute, message| -%>
<% if message.is_a?(String)%>
<li><%= message %></li>
<% end %>
<% end -%>
</ul>

请注意,这不会输出属性。您只需确保所有消息在没有属性名称的情况下都有意义。

关于ruby-on-rails - validates_presence_of + :message shows the name of the field,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2269183/

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