Rails 验证错误消息显示错误消息和数组 [英] Rails validation error messages displays the error messages and array

查看:35
本文介绍了Rails 验证错误消息显示错误消息和数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我创建了一个用户提交的表单以注册网站.

验证是在模型中设置的.它们已经过测试并且正在运行.

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

 <% if @user.errors.any?%><div id="error_explanation"><h2><%=pluralize(@user.errors.count, "error")%>禁止保存该用户:<p>以下字段存在问题:</p><ul><%= @user.errors.full_messages.each do |message|%><li><%=消息%></li><%结束%>

<%结束%>

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

2 错误禁止保存此用户:以下字段存在问题:密码不能为空密码太短(最少 6 个字符)**["密码不能为空", "密码太短(最少6个字符)"]**

对于我的生活,我无法弄清楚为什么.有人知道如何阻止它显示吗?

提前致谢.

这是 Rails3.0.7 Ruby 1.9.2 OSX10.6

解决方案

我用的不好

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

代替

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

I am working through the Ruby-on-Rails3 Tutorial Chapter 8.

I have created a form that a user submits to sign up to a site.

The validations are set in the model. They have been tested and are working.

The problem is that when I display the validation error messages doing the following:

    <% 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 %>

I not only get the validation messages but the actual array is shown as well:

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)"]**

For the life of me I can't figure out why. Anyone know how to stop this from displaying?

Thanks in advance.

This is Rails3.0.7 Ruby 1.9.2 OSX10.6

解决方案

My bad I used

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

instead of

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

这篇关于Rails 验证错误消息显示错误消息和数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆