2
votes

I have one problem with Ruby on Rails 4, Method create and Strong Parameters, when sending a post with nested values, my app save the object father correct, but the children save only create_at and update_at, below my code:

  1. My JSON sending

    {
    "CliRazao"=>"FORNECEDOR 1.", 
    "CliCNPJ"=>"78456896000185", 
    "CliEmail"=>"[email protected]", 
    "CliObs"=>"Teste\nTeste\nTEste", 
    "enderecos"=>[
        {
            "EndCEP"=>"17456789", 
            "EndTipo"=>"E", 
            "EndLogradouro"=>"RUA DOS TUCUNARES", 
            "EndNumero"=>"78", 
            "EndBairro"=>"JARDIM AQUARIUS", 
            "EndCidade"=>"MARILIA", 
            "EndEstado"=>"SP"
        }, 
        {
            "EndCEP"=>"18456123", 
            "EndTipo"=>"C", 
            "EndLogradouro"=>"RUA AFONSO PENA", 
            "EndNumero"=>"78", 
            "EndBairro"=>"JARDIM NOVO MUNDO", 
            "EndCidade"=>"MARILIA", 
            "EndEstado"=>"SP"
        }
    ], 
    "CliERP"=>"C-00125", 
    "cliente"=>
        {
            "CliERP"=>"C-00125", 
            "CliRazao"=>"FORNECEDOR 1", 
            "CliCNPJ"=>"78456896000185", 
            "CliEmail"=>"[email protected]", 
            "CliObs"=>"Teste\nTeste\nTEste"
        }
    

    }

  2. My Model cliente.rb (Father)

    class Cliente < ActiveRecord::Base has_many :enderecos, autosave: true

    accepts_nested_attributes_for :enderecos
    

    end

  3. My Model endereco.rb (Child)

    class Endereco < ActiveRecord::Base belongs_to :cliente end

  4. My controller

    class ClientesController < ApplicationController def index @clientes = Cliente.all end

    def findById
        @clientes = Cliente.find(params[:id]);
    end
    
    def create
        @cliente = Cliente.new(cliente_params);
        @cliente.enderecos.build(cliente_params[:enderecos_attributes]);
        logger.debug "Enderecos => #{cliente_params}"
    
        respond_to do |format|
            if @cliente.save
                format.json { render :show, status: :created }
            else
                format.json { render json: @cliente.errors, status: :unprocessable_entity }
            end
        end
    end
    
    def update
    end
    
    private
    def cliente_params
        params.require(:cliente).permit(:CliERP, :CliRazao, :CliCNPJ, :CliEmail, :CliObs, enderecos_attributes: [:EndTipo, :EndLogradouro, :EndNumero, :EndBairro, :EndCidade, :EndCEP, :EndEstado])
    end
    

    end

The enderecos are not inserted in my database and no exception is thrown.

Thanks!

1
Because the json what you sending has a wrong key "enderecos" => .... it should be "enderecos_attributes"=>.....Зелёный

1 Answers

0
votes

Your attributes aren't actually nested. If they were the json for enderecos would be inside cliente.

For nested attributes you need several things.

  1. You need accepts_nested_attributes_for, which you have.

  2. In our form you need to use fields_for for the nested attributes

  3. In your new action you need to build the nested resource

  4. In your controller you need to add the nested attributes to your permitted params with .permit(:enderecos_attributes => [:attribute_name, :another_attribute_name]

Hope it helps.