2014-12-02 34 views
0

我有2個選項卡(窗體)的頁面,它用於編輯一個對象。Rails在一個模型中的4個不同的驗證

E.g.其用戶信息。在第一個標籤中有個人信息,在下一個標籤中有帳單信息。

每個表單都有自己的提交按鈕。我如何分別驗證字段。如果我理解正確,那麼當我嘗試提交4個字段(10)的表單時,它會引發錯誤,其他字段(6/10)錯誤。

什麼是正確的方法來做到這一點?

回答

0

創建2個班,以反映您的UI:

class User 
    has_one :user_info 
    has_one :billing_info 
end 

class UserInfo 
    belongs_to :user 

    # add your validation 
end 

class BillingInto 
    belongs_to :user 

    # add your validation 
end 

在你的控制器:

def edit 
    @user = User.find(params[:id]) 
    @user_info = @user.build_user_info 
    @billing_info = @user.build_billing_info 
end 

然後在您的觀點:

= form_for @user_info do |f| 
= form_for @billing_info do |f| 

你需要2個控制器來處理請求POST。應分別命名爲UserInfosControllerUserBillingInfosController