Rails3:在多模型表单中显示验证错误?

时间:2012-05-21 08:24:55

标签: ruby-on-rails forms validation

是否可以创建这种形式,显示错误就像一个简单的模型? http://i.imgur.com/8t6ef.png

我创建了两个模型......但如果我填写错误的表单..错误消息将不会出现,错误Rails屏幕会说我,例如,“验证失败:field1不能为空... “

http://i.imgur.com/6KvVh.png

型号:

class Step < ActiveRecord::Base

  #validates
  validates :tree_id, :presence => true, :numericality => true
  validates :is_first, :presence => true, :length => {:maximum => 1}
  validates :status, :presence => true, :numericality => true
  validates :step_type_id, :presence => true

  #relations
  belongs_to :step_type
  belongs_to :tree
  has_many :statements

  accepts_nested_attributes_for :statements 

end

class Statement < ActiveRecord::Base

  #validates
  validates :step_id, :presence => true, :numericality => true
  validates :title, :presence => true, :length => {:maximum => 255}
  validates :statement, :presence => true
  validates :help, :presence => true
  validates :is_last_version, :presence => true, :length => {:maximum => 1}


  #relations
  belongs_to :step
  has_many :transitions

end

任何示例或建议?

1 个答案:

答案 0 :(得分:1)

您的视图中是否有以下几行?

<% if @statement.errors.any? %>
  <% flash[:notice] = "Please correct!" %>

    <% for message in @statement.errors.full_messages %>
        <li class="cf-messages-li"><%= message %></li>
    <% end %>

<% end %>