Rails 3.0已弃用f.error_messages,现在需要插件才能正常工作 - 但我想学习如何以(新)本机方式显示错误消息.我正在按照入门指南进行操作,该指南在实现注释表单时使用了已弃用的方法.例如:
<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 %>
Run Code Online (Sandbox Code Playgroud)
这是正确的方法(由脚手架生成):
<%= 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 …Run Code Online (Sandbox Code Playgroud) 这是一段很常见的代码
<% form_for :blah... do |f| %>
<%= f.error_messages %>
First name: <%= f.text_field :first_name %><br />
....
<% end %>
Run Code Online (Sandbox Code Playgroud)
error_messages 是一个帮助方法,但我很难找到它的文档,为什么会这样?