2013-04-15 36 views
1

這是一前一後重複項

我以前認爲這個問題繭相關的重新編輯,但現在我不這麼認爲,因爲下面的代碼沒有按甚至不會調用繭

每當我更新包含嵌套屬性的表單時,嵌套記錄的數量就會增加一倍。從我可以收集這種情況發生時,窗體調用,因爲我馬上可以看到更新之前,我做任何事情,形式呈現與重複的條目

我有我的下面HAML視圖相關的代碼 -

%h3 Household Members 
    = f.simple_fields_for :neighbors do |neighbor| 
    = render 'neighbor_fields', :f => neighbor 

我現在用體面的曝光與我的控制器,該控制器是這樣的:

class HouseholdsController < ApplicationController 

    expose(:households) 
    expose(:household, strategy: StrongParametersStrategy) 

    def create 
    if household.save 
     redirect_to households_path, notice: 'Household was successfully created.' 
    else 
    render 'new' 
    end 
end 

def update 
    if household.save 
    redirect_to households_path, notice: 'Household was successfully updated.' 
    else 
    render 'edit' 
    end 
end 

def destroy 
    household.destroy 

redirect_to households_path, notice: 'Household deleted.' 
end 

我怎樣才能讓我的嵌套屬性翻倍,從?

+0

我正在使用體面的曝光,所以我的控制器很簡單 - 我試過了,每次更新時嵌套屬性的數量都會翻倍 – tbrooke

+0

哪個版本的decent_exposure? – shingara

+0

你解決了這個問題嗎? – Kerozu

回答

3

我從來沒有使用過像樣的曝光,但我遇到過使用嵌套表格的問題,有和沒有繭,原因在兩種情況下都是一樣的。它必須處理強參數,而不是白名單中嵌套屬性的:id

我不知道我確切地得到你想要做的,所以我會給一個經典的職位/評論的例子。如果你有一個帖子的表單,並且你想動態地添加註釋字段,那麼你的控制器中的強大參數看起來就像這樣。

params.require(:post).permit(:content, comments_attributes: [:id, :content, :_destroy]) 

您需要白名單:id:_destroy,以及其它屬性的嵌套字段。如果沒有與評論相關聯的:id,那麼rails認爲這是一條新評論,併爲它創建一個新記錄。當你列出:id,然後Rails知道它是一個現有的對象,然後只是更新它。