gpt4 book ai didi

ruby-on-rails-3 - 如何从 Rails 3 中的错误消息中删除 "base: "一词?

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

这是在我的模型中调用我想要的错误的方式:

validate :number_of_clients

def number_of_clients
errors[:base] << "You cannot add another client" if
Authorization.current_user.plan.num_of_clients <= Authorization.current_user.clients.count
end

即使我将错误行更改为以下内容,它也会产生相同的结果:
errors.add(:base, "You cannot add another client")
errors.add_to_base("You cannot add another client")

这是错误消息的解析方式(在 _error_messages 部分中):
<% if object.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(object.errors.count, "error") %>
prohibited this user from being saved:</h2>
<p> There were problems with the following fields:</p>
<ul>
<% object.errors.full_messages.each_full do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>

这是错误消息在我的 html 中显示的方式:
There were errors: base: You cannot
add another client

编辑 1:我想要发生的是摆脱实际消息中的文本“base:”。所以我想说 There were errors: You cannot add another client .

最佳答案

试试

errors.add_to_base("Your message")

关于ruby-on-rails-3 - 如何从 Rails 3 中的错误消息中删除 "base: "一词?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5056917/

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