2017-12-28 19 views
0

Ich verwende Devise in meiner Ruby on Rails-Anwendung. Wenn Benutzer sich anmelden oder ihr Konto aktualisieren, möchte ich auch ihre AddressInformation erstellen/aktualisieren.Rails 5, verschachtelte Attribute, unerlaubte Parameter

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

Mein _form.html.haml sieht wie folgt aus:

= 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 

Ich habe versucht, die Attribute wie folgt hinzuzufügen:

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 

Wenn ich versuche, den Benutzer ich folgendes erhalten zu aktualisieren Fehler:

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

Irgendwelche Ideen, was ich vermisse?

+0

Vielleicht nur ein Tippfehler? Der nicht erlaubte Parameter ist ': address_informations', aber Sie erlauben': address_information' in 'configure_account_update_params'. –

+0

@DerekHopper Ich habe versucht, 'address_informations:' auch zu verwenden, aber es gibt das gleiche Ergebnis. – Anders

Antwort

2

In Ihrer Formulardefinition ist der Ressourcenname

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

in Plural, da Sie Attribute für :address_information erwarten Sie es

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

auch ändern sollten, wenn sie mit strong parameters and nested attributes arbeiten Sie die anhängen sollte _attributes Suffix zum Attributnamen - 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

Danke, als ich zu '= f.fields_for: Adressinformationen, resource.address_information do | address_field |' in meiner '_form.html.haml' gewechselt habe, werden meine verschachtelten Felder überhaupt nicht angezeigt. – Anders

+2

Beachten Sie, dass resource.address_information in diesem Fall null war, stattdessen habe ich verwendet: 'f.fields_for: address_information, resource.address_information? resource.address_information: resource.build_address_information do | address_field | ' – Anders

Verwandte Themen