我正在尝试使用字符串参数来允许传入模型的嵌套属性,无论出于什么原因,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
如果我传入格式正确的数据,则allowed_user_params将不会返回first_name, last_name, password and password_confirmation
以外的任何值。
谁能告诉我我做错了什么?
由于
答案 0 :(得分: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
的键,如果找不到它只是继续进行的任何操作,并且不会为您清理这些属性。