2015-07-13 115 views
2

我已經嘗試使用白名單標識,但它無濟於事,無論如何都會創建重複項。Rails 4在更新嵌套屬性時創建重複項

型號:

accepts_nested_attributes_for :address, :social, :contact, :talent_parameter 

我傳遞的屬性:

model_attributes = { 
      talent_parameter_attributes: { 
      }, 
      contact_attributes: { 
       agency_link: base_url + href 
      }, 
      social_attributes: { 
      }, 
      address_attributes: { 
      } 
     } 

     update_model(model, model_attributes) 

我設置的權限:

def self.update_model(model, attrs) 
    params = ActionController::Parameters.new(model: attrs) 
    model_params = params.require(:model) 
    model_params = model_params.permit(
     :company, 
     :age, 
     :avatar, 
     :gender, 
     :contact_id, 
     talent_features: [], 
     talent_parameter_attributes: [:id, :weight_lbs, :dress, :shoe, :chest, :waist, :hips, :height_ft], 
     contact_attributes: [:id, :agency_link], 
     social_attributes: [:id] 
    ) 

    model.update(model_params) 
    end 

我不明白。每次創建talent_parameter,contact,socialaddress的另一個副本。它有什麼問題?

回答

3

您是否包含id需要更新的機型?在Rails API,它指出:「對於那些不具有ID鍵的新紀錄將被實例化每個散」 ......

那麼試試這個:

model_attributes = { 
     talent_parameter_attributes: { 
     }, 
     contact_attributes: { 
      id: 7, 
      agency_link: base_url + href 
     }, 
     social_attributes: { 
     }, 
     address_attributes: { 
     } 
    }