Rails 3.0 中的 f.error_messages
[英] f.error_messages in Rails 3.0
本文介绍了Rails 3.0 中的 f.error_messages的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Rails 3.0 已弃用 f.error_messages
,现在需要插件才能正常工作 - 但是我想学习如何以(新的)原生方式显示错误消息.我正在关注 入门指南,它在实现评论表单时使用了已弃用的方法.例如:
添加评论:
<%= form_for([@post, @post.comments.build]) 做 |f|%><%= f.error_messages %><div class="field"><% f.label :commenter %><br/><%= f.text_field :commenter %>
<div class="field"><%= f.label :body %><br/><%= f.text_area :body %>
<div class="actions"><%= f.submit %>
<%结束%>
这是正确的方法(由脚手架生成):
<%= form_for(@post) do |f|%><% 如果@post.errors.any?%><div id="error_explanation"><h2><%=pluralize(@post.errors.count, "error") %>禁止保存此帖子:</h2><ul><% @post.errors.full_messages.each 做 |msg|%><li><%=msg%></li><%结束%>
<%结束%>...
我知道我在后一个例子中使用了 @post
变量,但是我在前一个例子中引用了什么变量来获取创建评论的错误消息?
解决方案
我刚刚查看了 docrails github 问题,他们决定删除 f.error_messages 而不是解释如何对评论进行验证.
Rails 3.0 deprecated f.error_messages
and now requires a plugin to work correctly - I however want to learn how to display error messages the (new) native way. I am following the getting started guide, which uses the deprecated method when implementing the comments form. For example:
<h2>Add a comment:</h2>
<%= form_for([@post, @post.comments.build]) do |f| %>
<%= f.error_messages %>
<div class="field">
<% f.label :commenter %><br />
<%= f.text_field :commenter %>
</div>
<div class="field">
<%= f.label :body %><br />
<%= f.text_area :body %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
Here is the correct way to do it (as generated by the scaffold):
<%= form_for(@post) do |f| %>
<% if @post.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@post.errors.count, "error") %> prohibited this post from being saved:</h2>
<ul>
<% @post.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
. . .
I understand that I use the @post
variable in the latter example, but what variable do I reference in the former to get the error messages for comment creation?
解决方案
I just looked into the docrails github issues, and they've decided to remove f.error_messages instead of explaining how to do validation for comments.
这篇关于Rails 3.0 中的 f.error_messages的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!