Ruby on rails RubyonRails中的错误消息

Ruby on rails RubyonRails中的错误消息,ruby-on-rails,Ruby On Rails,我对中的注释验证了字段的存在性。如何在posts页面上显示验证错误。我知道render,但使用render时,我需要发送我在posts页面上使用的所有变量。我说的对吗?验证的存在性,并使用:message参数自定义错误消息 class Person < ActiveRecord::Base validates_presence_of :user_name, :message => 'My custom error message' end class-Person“我的自定

我对中的注释验证了字段的存在性。如何在posts页面上显示验证错误。我知道render,但使用render时,我需要发送我在posts页面上使用的所有变量。我说的对吗?

验证的存在性,并使用:message参数自定义错误消息

class Person < ActiveRecord::Base
    validates_presence_of :user_name, :message => 'My custom error message'
end
class-Person“我的自定义错误消息”
结束

如图所示,您可以在
@variable.errors.full_messages
中访问这些验证错误(及其消息)。

如果您希望在视图中显示消息,如下所示:

这就是你想要的。。更多信息

`rails插件安装git://github.com/rails/dynamic_form.git`

rails 2.3中不需要插件(3.x中需要插件)

我认为不需要插件。Rails 3.x中的脚手架生成器是这样做的:

<%= form_for(@model) do |f| %>
  <% if @model.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@model.errors.count, "error") %> prohibited this model from being saved:</h2>

      <ul>
      <% @model.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>
<%# Rest of form... %>

禁止保存此模型:

如何在帖子页面上显示验证错误。我知道render,但使用render时,我需要发送我在posts页面上使用的所有变量。我说得对吗?
<%= form_for(@model) do |f| %>
  <% if @model.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@model.errors.count, "error") %> prohibited this model from being saved:</h2>

      <ul>
      <% @model.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>
<%# Rest of form... %>