3

Product類有price字段,它具有在Products表在數據庫中,和new_shop輔助字段適當的柱(其被定義爲attr_accessor,並在數據庫中不具有Products表中的適當列)。Rails 3:爲什麼錯誤的字段在驗證失敗時沒有用「field_with_errors」div封裝?

price驗證失敗,則輸入字段是包裹着field_with_errors格,但是當上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]

+0

請包括驗證碼 – kikito 2010-12-21 01:29:20

+0

我更新了問題。 – 2010-12-21 01:36:12

回答

3

所以它是: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. 
+0

非常感謝!一個小問題:「on」方法做了什麼?我沒有找到它的文檔。你能提請我嗎? – 2010-12-21 12:54:10

相關問題