【问题标题】:Rails3: Display validate errors in a multi-model Form?Rails3:在多模型表单中显示验证错误?
【发布时间】:2012-05-21 08:24:54
【问题描述】:

是否可以创建这种带有显示错误的表单,如简单模型? http://i.imgur.com/8t6ef.png

我创建了两个模型...但是如果我填写错误的表格...不会出现错误消息并且错误 Rails 屏幕会告诉我,例如“验证失败:字段 1 不能为空... "

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

有什么例子或建议吗?

【问题讨论】:

  • steps_controller.rb(第 59 行附近)你有什么?
  • 对不起,我解释的问题很糟糕,我正在尝试以嵌套形式查找信息。谢谢!

标签: ruby-on-rails forms validation


【解决方案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 %>

【讨论】:

  • 对不起,我解释的问题很糟糕,我正在尝试以嵌套形式查找信息。谢谢!
猜你喜欢
  • 2011-06-01
  • 1970-01-01
  • 2020-11-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-30
相关资源
最近更新 更多