2

I'm using Devise in my Ruby on Rails application. When users signs up or updates their account I also want to create / update their AddressInformation.

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

My _form.html.haml looks like this:

= 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

I have tried to add the attributes like this:

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

When I try to update the user I get the following error:

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

Any ideas on what I'm missing?

Anders
  • 2,903
  • 7
  • 58
  • 114
  • Maybe just a typo? The unpermitted parameter is `:address_informations` but you're permitting `:address_information` in `configure_account_update_params`. – Derek Hopper Dec 28 '17 at 20:40
  • @DerekHopper I've tried to use `address_informations:` as well, but it gives the same result. – Anders Dec 28 '17 at 20:43

1 Answers1

4

In your form definition, the resource name is in plural

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

since you are expecting attributes for :address_information you should change it to

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

Also when working with strong parameters and nested attributes you should attach the _attributes suffix to the attributes name - 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
Vane Trajkov
  • 758
  • 4
  • 11
  • Thanks, when I changed to `= f.fields_for :address_information, resource.address_information do |address_field|` in my `_form.html.haml` my nested fields don't get displayed at all. – Anders Dec 28 '17 at 20:53
  • 3
    Noticed that resource.address_information was nil in this case, I instead used: `f.fields_for :address_information, resource.address_information ? resource.address_information : resource.build_address_information do |address_field|` – Anders Dec 28 '17 at 21:03