2014-08-27 59 views
1

我想使用字符串參數來允許傳遞模型的嵌套屬性,無論出於何種原因permit方法忽略任何嵌套的屬性。請看下圖:嵌套屬性的強參數被忽略

型號:

class User < ActiveRecord::Base 
    accepts_nested_attributes_for :phone_number, :address, update_only: true 
    accepts_nested_attributes_for :profession, update_only: true 
end 

控制器:

class Api::V1::Users::UsersController < Api::BaseController 
    def update 
    permitted = permitted_user_params 
    end 

    def permitted_user_params 
    params.require(:profile).permit(
     :first_name, 
     :last_name, 
     :password, 
     :password_confirmation, 
     phone_number_attributes: [:phone_number], 
     address_attributes: [:street, :city, :province, :country, :postal_code], 
     profession_attributes: [:company, :title, :designation] 
    ) 
    end 
end 

如果我通過正確格式化數據permitted_user_params將不返回其他任何值比first_name, last_name, password and password_confirmation

誰能告訴我我做錯了什麼?

謝謝

+0

我想你忘記了嵌套模型的ID:'phone_number_attributes:[:身份證,電話號碼:PHONE_NUMBER], address_attributes:[:ID,:街道:城市:省:國家:POSTAL_CODE], profession_attributes: :id,:company,:title,:指定]' – Thanh 2014-08-28 08:57:35

+0

爲每個包含':id'似乎沒有任何作用。 – adampetrie 2014-08-28 13:23:36

+0

你有沒有想過這個呢?我看到同樣的事情。 – MFrazier 2015-02-03 00:50:36

回答

0

我想通了我的錯誤。當您發佈到端點時,您的數據需要與導軌側的預期格式相匹配。對於上面的例子中你需要像後您的數據:

{ 
    profile: { 
     first_name, 
     last_name, 
     password, 
     password_confirmation, 
     phone_number_attributes: { 
      phone_number 
     }, 
     address_attributes: { 
      street, 
      city, 
      province, 
      country, 
      postal_code 
     }, 
     profession_attributes: { 
      company, 
      title, 
      designation 
     } 
    } 
} 

Rails的明確查找與上月底的價值_attributes鍵,如果它不能找到任何東西,它只是進行和不消毒的屬性爲你。