Rails验证错误消息显示错误消息和数组



我正在学习Ruby-on-Rails3教程第8章。

我创建了一个表单,用户可以提交该表单来注册网站。

验证是在模型中设置的。它们已经过测试,正在发挥作用。

问题是,当我显示验证错误消息时,执行以下操作:

    <% if @user.errors.any? %>
    <div id="error_explanation">
        <h2>
            <%= pluralize(@user.errors.count, "error")%>
            prohibited this user from being saved:  
        </h2>
        <p>There were problems with the following fields:</p>
        <ul>
        <%= @user.errors.full_messages.each do |message| %>
            <li><%= message %></li>
        <% end %>
        </ul>   
    </div>
<% end %>

我不仅得到了验证消息,还显示了实际的数组:

2 errors prohibited this user from being saved:
There were problems with the following fields:
    Password can't be blank
    Password is too short (minimum is 6 characters)
    **["Password can't be blank", "Password is too short (minimum is 6 characters)"]**

我一辈子都不明白为什么。有人知道如何阻止它显示吗?

提前谢谢。

这是Rails3.0.7 Ruby 1.9.2 OSX10.6

  <%= @user.errors.full_messages.each do |message| %>
       <li><%= message %></li>

使用此行打印数组<%= @user.errors.full_messages.each do |message| %>会从中删除=

我坏了,我用了

<%=@user.errors.full_messages.each do |message|%> 

而不是

<% @user.errors.full_messages.each do |message|

相关内容

  • 没有找到相关文章

最新更新