简体   繁体   中英

Active Admin Nested Form Edit User Information

I have two models / resources in Raisl 4 / ActiveAdmin application.

class AdminUser < ActiveRecord::Base
   devise :database_authenticatable, :recoverable, :rememberable, :trackable, :validatable

   has_one :photographer
end

class Photographer < ActiveRecord::Base
   belongs_to :admin_user, dependent: :destroy
   accepts_nested_attributes_for :admin_user
end

ActiveAdmin.register Photographer do

   permit_params :code, :nickname, :profile, :facebook_url, :twitter_url, :instagram_url, :address, :complement, :zip_code, :city, :state, :country,
                 :phone, :cellphone, :commission, :withhold_tax, :bank_number, :bank_branch_number, :bank_account_number, :identity_document_number,
                 :rfb_document_number, admin_user_attributes: [:email, :password, :password_confirmation]

   form do |f|
      f.inputs for: [:admin_user, (f.object.admin_user || f.object.build_admin_user)] do |auf|
         auf.input :email
         auf.input :password
         auf.input :password_confirmation
      end
      f.inputs do
         f.input :code
         f.input :nickname
         f.input :profile
         f.input :facebook_url
         f.input :twitter_url
         f.input :instagram_url
         f.input :address
         f.input :complement
         f.input :zip_code
         f.input :city
         f.input :state
         f.input :country, as: :string
         f.input :phone
         f.input :cellphone
         f.input :commission
         f.input :withhold_tax
         f.input :bank_number
         f.input :bank_branch_number
         f.input :bank_account_number
         f.input :identity_document_number
         f.input :rfb_document_number
      end
      f.actions
   end

end

The process of creation / validation is working perfectly, however, when editing a Photographer without changing e-mail I get the error "Email has already been taken" as actually associated AdminUser record was being created and not edited.

I found the problem. For editing work properly you must also accept the Id parameter in the User attributes, otherwise it will try to create a new one.

admin_user_attributes: [:id, :email, :password, :password_confirmation]

Many thanks to @d34n5.

Strong parameters for nested attributes returns "unpermitted parameters" when empty array

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM