不允许的嵌套参数导轨

问题描述 投票:0回答:1

我遇到的问题是通过client_controller向我的数据库发送数据,客户端模型与client_address有has_many关系,当尝试提交数据时,注册了以下字段(client、personal_documents和client_bank_information),但在rails控制台apacere上没有将client_addresses保存到银行,这是不允许的(Unpermitted parameter::client_address_attributes),谢谢帮助!。

模型。

class Client < ApplicationRecord
    has_one :client_bank_information, :dependent => :destroy
    has_one :personal_document, dependent: :destroy
    has_many :client_addresses, :dependent => :destroy

    accepts_nested_attributes_for :client_bank_information, allow_destroy: true
    accepts_nested_attributes_for :personal_document, allow_destroy: true
    accepts_nested_attributes_for :client_addresses, allow_destroy: true
end

控制器。

class ClientsController < ApplicationController
  include ErrorSerializer

  # POST /clients
  def create
    @client = Client.new(client_params)
    if @client.save
      render json: @client, status: :created, location: @client
    else
      render json: ErrorSerializer.serialize(@client.errors)
    end
  end

private

  def client_params
    params.permit(
                                   :name, 
                                   :email, 
                                   :phone, 
                                   :cpf_cnpj, 
                                   :accept, 
                                   :version_term_accept, 
                                   :birth_date,
                                   personal_document_attributes: [:rg_front,
                                                                  :rg_verse,
                                                                  :cpf,
                                                                  :cnh_front,
                                                                  :cnh_verse,
                                                                  :bank_card_front,
                                                                  :proof_residence,
                                                                  :profile_photo
                                                                 ],
                                   client_bank_information_attributes: [:bank, 
                                                                        :account_type, 
                                                                        :agency, 
                                                                        :account_number
                                                                       ],
                                   client_address_attributes: [  
                                                                 :spot_to_clean, 
                                                                 :zip_code, 
                                                                 :street, 
                                                                 :street_number, 
                                                                 :complement, 
                                                                 :district, 
                                                                 :city, 
                                                                 :state
                                                                ]
    )
  end

end

控制台数据:

Parameters: {"name"=>"teste", "email"=>"teste", "phone"=>"teste", "cpf_cnpj"=>"132.635.357-81", "accept"=>"true", "version_term_accept"=>"teste", "personal_document_attributes"=>{"rg_front"=>#<ActionDispatch::Http::UploadedFile:0x0000564cd8bd9310 @tempfile=#<Tempfile:/tmp/RackMultipart20200610-7-1fozi8w.jpg>, @original_filename="aaaaaaaaaaaaaaaaaaaa.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"personal_document_attributes[rg_front]\"; filename=\"aaaaaaaaaaaaaaaaaaaa.jpg\"\r\nContent-Type: image/jpeg\r\n">, "rg_verse"=>#<ActionDispatch::Http::UploadedFile:0x0000564cd8bd9220 @tempfile=#<Tempfile:/tmp/RackMultipart20200610-7-1f3xcli.jpg>, @original_filename="aaaaaaaaaaaaaaaaaaaa.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"personal_document_attributes[rg_verse]\"; filename=\"aaaaaaaaaaaaaaaaaaaa.jpg\"\r\nContent-Type: image/jpeg\r\n">, "cpf"=>#<ActionDispatch::Http::UploadedFile:0x0000564cd8bd91a8 @tempfile=#<Tempfile:/tmp/RackMultipart20200610-7-1c2i2kg.jpg>, @original_filename="aaaaaaaaaaaaaaaaaaaa.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"personal_document_attributes[cpf]\"; filename=\"aaaaaaaaaaaaaaaaaaaa.jpg\"\r\nContent-Type: image/jpeg\r\n">, "cnh_front"=>#<ActionDispatch::Http::UploadedFile:0x0000564cd8bd9108 @tempfile=#<Tempfile:/tmp/RackMultipart20200610-7-1o60s5q.jpg>, @original_filename="aaaaaaaaaaaaaaaaaaaa.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"personal_document_attributes[cnh_front]\"; filename=\"aaaaaaaaaaaaaaaaaaaa.jpg\"\r\nContent-Type: image/jpeg\r\n">, "cnh_verse"=>#<ActionDispatch::Http::UploadedFile:0x0000564cd8bd9040 @tempfile=#<Tempfile:/tmp/RackMultipart20200610-7-1lfwhtu.jpg>, @original_filename="aaaaaaaaaaaaaaaaaaaa.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"personal_document_attributes[cnh_verse]\"; filename=\"aaaaaaaaaaaaaaaaaaaa.jpg\"\r\nContent-Type: image/jpeg\r\n">, "bank_card_front"=>#<ActionDispatch::Http::UploadedFile:0x0000564cd8bd8fc8 @tempfile=#<Tempfile:/tmp/RackMultipart20200610-7-1j6h05v.jpg>, @original_filename="aaaaaaaaaaaaaaaaaaaa.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"personal_document_attributes[bank_card_front]\"; filename=\"aaaaaaaaaaaaaaaaaaaa.jpg\"\r\nContent-Type: image/jpeg\r\n">, "proof_residence"=>#<ActionDispatch::Http::UploadedFile:0x0000564cd8bd8f50 @tempfile=#<Tempfile:/tmp/RackMultipart20200610-7-1hjnpnd.jpg>, @original_filename="aaaaaaaaaaaaaaaaaaaa.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"personal_document_attributes[proof_residence]\"; filename=\"aaaaaaaaaaaaaaaaaaaa.jpg\"\r\nContent-Type: image/jpeg\r\n">, "profile_photo"=>#<ActionDispatch::Http::UploadedFile:0x0000564cd8bd8eb0 @tempfile=#<Tempfile:/tmp/RackMultipart20200610-7-1lrwru3.jpg>, @original_filename="aaaaaaaaaaaaaaaaaaaa.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"personal_document_attributes[profile_photo]\"; filename=\"aaaaaaaaaaaaaaaaaaaa.jpg\"\r\nContent-Type: image/jpeg\r\n">}, "client_bank_information_attributes"=>{"bank"=>"teste", "agency"=>"teste", "account_type"=>"CA", "account_number"=>"teste"}, "client_address_attributes"=>{"spot_to_clean"=>"teste", "zip_code"=>"teste", "street"=>"teste", "street_number"=>"4", "complement"=>"teste", "district"=>"teste", "city"=>"teste", "state"=>"teste"}, "birth_date"=>"23/11/2000"}
project          | Can't verify CSRF token authenticity.
project          | Unpermitted parameter: :client_address_attributes
project          |    (0.9ms)  BEGIN
project          |   ↳ app/controllers/clients_controller.rb:29:in `create'
project          |   Client Create (1.5ms)  INSERT INTO `clients` (`name`, `email`, `phone`, `cpf_cnpj`, `accept`, `version_term_accept`, `birth_date`, `created_at`, `updated_at`) VALUES ('teste', 'teste', 'teste', '132.635.357-81', TRUE, 'teste', '2000-11-23', '2020-06-10 03:31:45.466950', '2020-06-10 03:31:45.466950')
project          |   ↳ app/controllers/clients_controller.rb:29:in `create'
project          |   ClientBankInformation Create (0.8ms)  INSERT INTO `client_bank_informations` (`bank`, `account_type`, `agency`, `account_number`, `created_at`, `updated_at`, `client_id`) VALUES ('teste', 'CA', 'teste', 'teste', '2020-06-10 03:31:45.492594', '2020-06-10 03:31:45.492594', 2)
project          |   ↳ app/controllers/clients_controller.rb:29:in `create'
project          |   PersonalDocument Create (4.9ms)  INSERT INTO `personal_documents` (`rg_front`, `rg_verse`, `cpf`, `cnh_front`, `cnh_verse`, `bank_card_front`, `proof_residence`, `profile_photo`, `created_at`, `updated_at`, `client_id`) VALUES ('aaaaaaaaaaaaaaaaaaaa.jpg', 'aaaaaaaaaaaaaaaaaaaa.jpg', 'aaaaaaaaaaaaaaaaaaaa.jpg', 'aaaaaaaaaaaaaaaaaaaa.jpg', 'aaaaaaaaaaaaaaaaaaaa.jpg', 'aaaaaaaaaaaaaaaaaaaa.jpg', 'aaaaaaaaaaaaaaaaaaaa.jpg', 'aaaaaaaaaaaaaaaaaaaa.jpg', '2020-06-10 03:31:45.521734', '2020-06-10 03:31:45.521734', 2)
ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-4 ruby-on-rails-5
1个回答
0
投票

你的问题出在你的表单上,但你没有发布表单内容,所以我无法按照你问题的评论要求,在没有这些信息的情况下为你解决这个问题。

您的 fields_for 内的设置不正确。form_with 视图模板中的代码块作为 client_address_attributes params打到controller action时,params似乎没有正确地嵌套在client params里面,你正确地指定它们应该在 params.permit 声明

但你的问题比这更深,因为你也有一个。Can't verify CSRF token authenticity 需要解决的错误,可能与以下原因有关

© www.soinside.com 2019 - 2024. All rights reserved.