Rails API 嵌套属性中不允许的参数
Unpermitted parameter in Rails API nested attributes
我正在尝试使用嵌套字段更新对象,但收到 Unpermitted parameters
错误。导致错误的字段本身与嵌套 table 中的另一个 table 相关。具体内容如下:
医生class
class Doctor < User
has_many :professional_licenses, dependent: :destroy
has_many :states, through: :professional_licenses
accepts_nested_attributes_for :professional_licenses, allow_destroy: true
...
end
专业执照class
class ProfessionalLicense < ApplicationRecord
belongs_to :doctor
belongs_to :state
validates_presence_of :code
end
状态class
class State < ActiveRecord::Base
validates_presence_of :iso_abbr, :name
end
博士控制器
...
def update
doctor = @current_user
params[:doctor][:professional_licenses_attributes].each do |license, index|
license[:state] = State.find_by_iso_abbr license[:state]
end
doctor.update_attributes(doctor_params)
render json: doctor, status: :ok
end
...
def doctor_params
params.require(:doctor).permit(:email, :first_name, :last_name, :password,
:password_confirmation, professional_licenses_attributes: [:code, :state, :_destroy])
end
来自 UI 的调用如下所示:
{
"doctor":{
"first_name":"Doctor Postman",
"professional_licenses_attributes": [
{
"code": "NY-1234",
"state": "NY"
},
{
"code": "MA-1234",
"state": "MA"
}
]
}
}
当我发送呼叫时,正在更新记录并创建许可证。但是,创建的许可证没有状态,因为控制器显示 Unpermitted parameters: state
。我尝试了不同的方法,但找不到允许状态的方法。请帮忙!
在您的例子中,code
参数应该是一个简单的值,例如整数或字符串。但是你把它转换成一个对象,它的属性也必须添加到允许列表中。
尝试传递 code_id
(整数)而不是 code
(对象):
...
def update
doctor = @current_user
params[:doctor][:professional_licenses_attributes].each do |license|
state = State.find_by_iso_abbr(license.delete(:state))
license[:state_id] = state.id if state
end
doctor.update_attributes(doctor_params)
render json: doctor, status: :ok
end
...
def doctor_params
params.require(:doctor).permit(:email, :first_name, :last_name, :password,
:password_confirmation, professional_licenses_attributes: [:code, :state_id, :_destroy])
end
我正在尝试使用嵌套字段更新对象,但收到 Unpermitted parameters
错误。导致错误的字段本身与嵌套 table 中的另一个 table 相关。具体内容如下:
医生class
class Doctor < User
has_many :professional_licenses, dependent: :destroy
has_many :states, through: :professional_licenses
accepts_nested_attributes_for :professional_licenses, allow_destroy: true
...
end
专业执照class
class ProfessionalLicense < ApplicationRecord
belongs_to :doctor
belongs_to :state
validates_presence_of :code
end
状态class
class State < ActiveRecord::Base
validates_presence_of :iso_abbr, :name
end
博士控制器
...
def update
doctor = @current_user
params[:doctor][:professional_licenses_attributes].each do |license, index|
license[:state] = State.find_by_iso_abbr license[:state]
end
doctor.update_attributes(doctor_params)
render json: doctor, status: :ok
end
...
def doctor_params
params.require(:doctor).permit(:email, :first_name, :last_name, :password,
:password_confirmation, professional_licenses_attributes: [:code, :state, :_destroy])
end
来自 UI 的调用如下所示:
{
"doctor":{
"first_name":"Doctor Postman",
"professional_licenses_attributes": [
{
"code": "NY-1234",
"state": "NY"
},
{
"code": "MA-1234",
"state": "MA"
}
]
}
}
当我发送呼叫时,正在更新记录并创建许可证。但是,创建的许可证没有状态,因为控制器显示 Unpermitted parameters: state
。我尝试了不同的方法,但找不到允许状态的方法。请帮忙!
在您的例子中,code
参数应该是一个简单的值,例如整数或字符串。但是你把它转换成一个对象,它的属性也必须添加到允许列表中。
尝试传递 code_id
(整数)而不是 code
(对象):
...
def update
doctor = @current_user
params[:doctor][:professional_licenses_attributes].each do |license|
state = State.find_by_iso_abbr(license.delete(:state))
license[:state_id] = state.id if state
end
doctor.update_attributes(doctor_params)
render json: doctor, status: :ok
end
...
def doctor_params
params.require(:doctor).permit(:email, :first_name, :last_name, :password,
:password_confirmation, professional_licenses_attributes: [:code, :state_id, :_destroy])
end