ruby-on-rails - rails 3 : Why the field with error is not wrapped with "field_with_errors" div when validation fails?

标签 ruby-on-rails validation ruby-on-rails-3 field-with-errors

我的 Product 类有 price 字段,它在数据库的 Products 表中有一个适当的列,并且 new_shop 帮助字段(定义为 attr_accessor,并且在数据库的 Products 表中没有合适的列)。

price 验证失败时,输入字段被包裹在 field_with_errors div 中,但是当 new_shop 验证失败时,它不是用 field_with_errors div 包装。为什么?

这是为这些输入字段生成的 HTML:

<input type="text" name="product[price]" id="product_price">
<input type="text" value="" name="product[new_shop]" id="product_new_shop">

更多信息:

class Product < ActiveRecord::Base
  attr_accessor :new_shop 
  accepts_nested_attributes_for :shop
  validates_presence_of :price
  ...
end

class Shop < ActiveRecord::Base
  validates_presence_of :name
  ...
end

提交表单时,new_shop 值将传递给产品的 shop_attributes[:name]

最佳答案

所以实际上验证失败的是 :name 属性?这就是为什么 new_shop 没有得到 fieldWithErrors div 的原因:这会查看 @product.errors 以逐个字段地决定它是否有错误。即

#comes to do the :new_shop field
#looks to see if @product.errors.on(:new_shop) is not blank
#if it isn't blank, wraps the error div round it. 

关于ruby-on-rails - rails 3 : Why the field with error is not wrapped with "field_with_errors" div when validation fails?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4495539/

相关文章:

ruby-on-rails - 嵌套属性未插入表中

ruby-on-rails - 使用 Ruby Mechanize 登录亚马逊合作伙伴网

css - Drupal 7 表单验证失败后缺少 css 规则

ruby-on-rails - 如何弃用 Rails 中的静态方法?

ruby-on-rails-3 - Rails 3 - 多个 Controller 之间共享的代码 - 将其放在哪里?

ruby-on-rails - 无法批量分配 protected 属性 : confirmed_at

ruby-on-rails - 如何在 Rails 4.2 中扩展模型

c# - 使用 ValidationException 类

javascript - MVC3 不显眼的验证 : how to remove/re-attach validation from a group of elements?

ruby-on-rails - 如何向 Rails 的部分渲染查找添加 View 路径?