2017-12-28 6 views
0

Ruby on Rails 응용 프로그램에서 Devise를 사용하고 있습니다. 사용자가 자신의 계정에 가입하거나 업데이트 할 때 AddressInformation을 생성/업데이트하려고합니다.레일 5, 중첩 된 속성을 허용, 허용되지 않은 매개 변수

class User < ApplicationRecord belongs_to :address_information accepts_nested_attributes_for :address_information, allow_destroy: true [...]

_form.html.haml은 다음과 같습니다 : 내가 얻을 사용자를 업데이트하려고하면 다음과 같은

class Users::RegistrationsController < Devise::RegistrationsController 
    before_action :configure_sign_up_params, only: [:create] 
    before_action :configure_account_update_params, only: [:update] 

    [...] 

    # If you have extra params to permit, append them to the sanitizer. 
    def configure_account_update_params 
    devise_parameter_sanitizer.permit(:account_update, keys: [ 
     :name, 
     address_information: [ 
     :address, 
     :care_of, 
     :zip_code, 
     :country, 
     :state 
     ] 
    ]) 
    end 

:이 같은 속성을 추가하기 위해 노력했다

= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| 
    = devise_error_messages! 
    .form-group 
    = f.label :name 
    = f.text_field :name 

    = f.fields_for :address_informations, resource.address_information do |address_field| 
    .form-group 
     = address_field.label :address 
     = address_field.text_field :address 
    .form-group 
     = address_field.label :care_of 
     = address_field.text_field :care_of 
    .form-group 
     = address_field.label :zip_code 
     = address_field.text_field :zip_code 
    .form-group 
     = address_field.label :city 
     = address_field.text_field :city 

오류 :

Unpermitted parameter: :address_informations 
(0.2ms) BEGIN 
(0.2ms) ROLLBACK 

내가 누락 된 항목에 대한 아이디어가 있으십니까? 당신이를 첨부해야 strong parameters and nested attributes 작업 할 때 또한

= f.fields_for :address_information, resource.address_information do |address_field| 

로 변경해야 :address_information 속성을 기대하고 있기 때문에 양식 정의에서

+0

그냥 오타 일 수도 있습니다. 허용되지 않은 매개 변수는': address_informations'이지만'configure_account_update_params'에': address_information'을 허용합니다. –

+0

@DerekHopper'address_informations :'도 사용하려고했지만 동일한 결과가 나온다. – Anders

답변

2

는 자원 이름은 복수

= f.fields_for :address_informations, resource.address_information do |address_field| 

에 속성 이름 접미사 _attributes - address_information_attributes

def configure_account_update_params 
    devise_parameter_sanitizer.permit(:account_update, keys: [ 
     :name, 
     address_information_attributes: [ 
     :address, 
     :care_of, 
     :zip_code, 
     :country, 
     :state 
     ] 
    ]) 
    end 
+0

감사합니다. 내'_form.html.haml'에서'= f.fields_for : address_information, resource.address_information do | address_field | '로 변경했을 때 중첩 필드가 전혀 표시되지 않습니다. – Anders

+2

이 경우 resource.address_information이 nil 인 것으로 나타났습니다. 대신에 : f.fields_for : address_information, resource.address_information? resource.address_information : resource.build_address_information do | address_field |' – Anders

관련 문제