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

时间:2011-06-09 07:29:36

标签: ruby-on-rails ruby-on-rails-3 railstutorial.org

我正在使用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

2 个答案:

答案 0 :(得分:37)

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

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

答案 1 :(得分:-16)

我的坏事我用了

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

而不是

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