2016-07-27 71 views
0

我們有導軌模型活動,它有has_many位置和camapign也有許多組和團體有很多標籤 我們喜歡所有驗證必須與服務器端。如何驗證rails窗體如果將多窗體中的窗體拆分?

我的車型有:

class Camapign < ActiveRecord::Base 
    has_many :locations 
    has_many :groups 
    attr_writer :current_tab 
    validates :name, :presence => true 
    validates :event_date, :presence => true 
end 

class Location < ActiveRecord::Base 
    validates :name, :presence => true 
end 

class Group < ActiveRecord::Base 
    has_many :tags 
    validates :name, :presence => true 
    validates :industry, :presence => true 
end 

class Tag < ActiveRecord::Base 
    validates :name, :presence => true 
end 

當我創建Camapign我與驗證虛假創建Camapign和我的看法是不同的標籤/門戶。

我需要驗證只有我已經在窗體上顯示的字段,如果它無效,那麼它應該導航到適當的錯誤尊重選項卡。

第一個選項卡,我有系列的名稱和第二個選項卡上的位置 我有團體名稱和標籤上

我要保存所有標籤細節與尊重的進步數據庫選項卡,並在年底符合並公佈所有細節但我想驗證細節吃中間選項卡,我有模態。

我有關於Camapign模型的所有操作與關係插入,我跟蹤current_tab屬性。

我能夠使用current_tab值驗證Camapign模態字段,但無法驗證其他關係模態。

我們不喜歡使用像邪惡的任何額外的寶石,我喜歡做像標準代碼一樣簡單,所以我們有較少的依賴性。

請幫助我們驗證基於選項卡和鋤頭的表單,以在選項卡上顯示錯誤。

回答

0

我們可以通過之前的驗證節省事件

class Camapign < ActiveRecord::Base 
    has_many :locations 
    has_many :groups 
    attr_writer :current_tab 
    validates :name, :presence => true 
    validates :event_date, :presence => true 

attr_writer :current_step # for access current tabs 

     with_options :if => Proc.new { |campaign| campaign.current_step == "location"} do |step| 
      step.after_validation :validate_location_name 
      end 

    def validate_location_name 
     self.locations.each do |location| 
      if !location.name.present? 
      location.errors.add(:name,"Invalid location") 
      errors.add(:name,"Invalid location") 
      end 
     end 
     end 
end