2014-08-27 58 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键,如果它不能找到任何东西,它只是进行和不消毒的属性为你。