Ruby on rails 如何去掉Rails模型错误消息周围出现的括号?

Ruby on rails 如何去掉Rails模型错误消息周围出现的括号?,ruby-on-rails,validation,model,ruby-on-rails-5,Ruby On Rails,Validation,Model,Ruby On Rails 5,我使用的是Rails 5。在我的模型中,如果我的一个字段无效,我会设置一个错误 errors.add(:my_field, 'The field is not in the correct format') 然后在我看来,我显示的错误如下 <% if !@user.errors[:my_field].empty? %><span class="profileError"> <%= @user.errors[:my_field] %></span>

我使用的是Rails 5。在我的模型中,如果我的一个字段无效,我会设置一个错误

errors.add(:my_field, 'The field is not in the correct format')
然后在我看来,我显示的错误如下

<% if !@user.errors[:my_field].empty? %><span class="profileError"> <%= @user.errors[:my_field] %></span><% end %>  

如何去掉错误周围出现的括号?这似乎是一个非常简单的问题,但我不知道这些东西是如何潜入其中的。

@user.errors[:my_字段]
是一组错误消息

要显示所有错误,您可以执行以下操作

@user.errors[:my_field].join(', ')
这将显示预期的单个错误,以及由逗号分隔的多个错误

e、 g

变成

not an integer, not less than ten
not an integer

变成

not an integer, not less than ten
not an integer

在Rails中,任何给定属性的错误都是一个数组,因为一个属性可能会多次验证失败

通常使用
@user.errors.full_消息
,然后迭代所有错误消息:

<% if @user.errors.any? %>
<ul>
  <%= @user.errors.full_messages.each do |m| %>
  <li><%= m %></li> 
  <% end %>
</ul>
<% end %>

在您的情况下,可以通过特定键进行迭代:

<% @user.errors[:my_field].each do |msg| %>
  <span class="profileError"><%= msg %></span>
<% end if @user.errors[:my_field].any? %>


根据所需输出的内容,您还可以对(:my_字段)使用
完整消息。有关更多示例,请参阅文档。

将此
@user.errors[:my_字段]
更改为
@user.errors[:my_字段]。首先

<% @user.errors[:my_field].each do |msg| %>
  <span class="profileError"><%= msg %></span>
<% end if @user.errors[:my_field].any? %>