2013-04-24 139 views
0

我正在研究Rails 3.2應用程序上的ruby,我需要驗證帶有嵌套屬性的窗體。在我的模型我有這個Ruby on rails 3.2 accept_nested_attributes驗證

 
class Member 
    attr_accessible :email, :password.............etc 
    has_many :purchase_informations, :dependent => :destroy 
    accepts_nested_attributes_for :purchase_informations, :allow_destroy => true 
end 

class PurchaseInformation 
    belongs_to :member 
    attr_accessor :same_billing 
end 

及以下表單數據:

{ members: 
    { purchase_informations_attributes: 
      { 0: 
       { information_type: "billing", 
       title: "Mr", 
       first_name: "", 
       last_name: "", 
       cuhk_no: "", 
       organization: "", 
       address: "", 
       zip_code: "", 
       country: "Hong Kong", 
       telephone: "", 
       mobile: "", fax: "", 
       email: "asdasdasda", id: "27" 
       }, 
      1: 
       { information_type: "shipping", 
       title: "Mr", 
       first_name: "", 
       last_name: "", 
       cuhk_no: "", 
       organization: "", 
       address: "", 
       zip_code: "", 
       country: "Hong Kong", 
       telephone: "", 
       mobile: "", 
       fax: "", 
       email: "", 
       id: "28" 
       } 
     } 
    } 
} 

請幫我驗證這個屬性是空白。

謝謝!

回答

0

你好你可以試試這個:

accepts_nested_attributes_for : purchase_informations, :reject_if => :all_blank 

或在您的形式把一個:要求=>真正的功能

<%= f.input :name, :required => true %> 
+0

嗨艾倫。感謝您的洞察力,不幸的是上面的方法不是一個選項,因爲我需要遍歷每個值來檢查是否有空值。只有所有值都是空白的,它纔會起作用。對於輸入字段中的必需屬性,這用於html 5,並非所有瀏覽器都支持這一點。非常感謝你。 – Azrael1932 2013-04-24 11:15:02

+0

嘗試此操作:此操作驗證特定列並​​驗證是否存在嵌套屬性的名稱: accepting_attributes_for:purchase_informations,:reject_if => lambda {| a | a [:name] .blank? } – 2013-04-25 03:58:29