중첩 된 필드를 사용하여 개체를 업데이트하려고하는데 Unpermitted parameters
오류가 발생합니다. 오류를 일으키는 필드 자체는 중첩 테이블 내의 다른 테이블과의 관계입니다. 다음은 세부 사항입니다.
닥터 클래스
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 ProfessionalLicense < ApplicationRecord
belongs_to :doctor
belongs_to :state
validates_presence_of :code
end
상태 클래스
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
매개 변수는 정수 또는 문자열과 같은 간단한 값이 될 것으로 예상됩니다. 그러나이를 개체로 변환하면 해당 속성도 허용 목록에 추가되어야합니다.
(object) code_id
대신 (integer) 전달을 시도하십시오 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
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다