Nested Attributes Rails child model not saved

2

Good afternoon!

I'm having a problem storing templates with nested attributes .

In the app we have Customer , which has 1..n Contacts , which in turn has 1..n Telephones .

I researched a lot before putting it here and decided to only work with Contact before. Well, at first Customer is saved, but Contact does not. From what I read, you do not need to repeat the .build child model in the create function, and the "@ customer = Customer.new customer_params) " would create and save both. After searching the error a lot, I found that in the parameters that came from form , the hash needed for the Contact registry came with the following structure: : contacts_attributes [: 0 [... (correct data]] " ie the data has been encapsulated within another hash. Because of this, my other solution did not work (Put the contacts.build (customer_params [: contacts_attributes]) more). I guess this is in case I add more than one Contact at a time.

I could not find a clear explanation of how this works, but I came up with the following codes:

customer.rb

class Customer < ActiveRecord::Base
...    
has_many :contacts
accepts_nested_attributes_for :contacts, reject_if: lambda {|attributes| attributes['kind'].blank?}
... 
def change_by(user_id)
  update_attributes(changed_by: user_id, deleted_at: Time.now, updated_at: Time.now)
end

def delete(user_id)
  update_attributes(status: false, changed_by: user_id, deleted_at: Time.now, updated_at: Time.now)
end

private
...
end

Customers_controller.rb

class CustomersController < ApplicationController 

def new
  @customer = Customer.new
  @customer.contacts.new
end

def create
  user_id = session[:user_id]
  @customer = Customer.new(customer_params)
  if @customer.save
    @customer.change_by(user_id)
    flash[:success] = "Cliente cadastrado com sucesso!"
    redirect_to customers_url
  else
    render 'new'
  end
end

private

def customer_params
  params.require(:customer).permit(:razao_social, :nome, :CPF_CNPJ,
              :adress_id, :email_nota, :transporter_id, :observacao, 
              contacts_attributes: [:nome, :setor, :email])
end

Entry Form

<div class="row">
<div class="col-md-6 col-md-offset-3">
  <%= form_for @customer do |f| %>
    <%= f.label "Dados Básicos" %>
    <div class="well">      
    <%= f.label :razao_social, "Razão Social" %>
    <%= f.text_field :razao_social %>

    <%= f.label :nome, "Nome" %>
    <%= f.text_field :nome %>

    <%= f.label :CPF_CNPJ, "CPF/CNPJ" %>
    <%= f.text_field :CPF_CNPJ %>

    <%= f.label :email_nota, "Email para nota" %>
    <%= f.email_field :email_nota %>

    <%= f.label :observacao, "Observações" %>
    <%= f.text_area :observacao %>
    </div>

    <%= f.fields_for :contacts do |k| %>
      <%= k.label "Contato" %>
      <div class="well">   
        <%= k.label :nome, "Nome" %>
        <%= k.text_field :nome %>

        <%= k.label :setor, "Setor" %>
        <%= k.text_field :setor %>

        <%= k.label :email, "Email" %>
        <%= k.email_field :email %>

      </div>   
    <% end %>
    <%= f.submit "Cadastrar Cliente", class: "btn btn-primary" %>
  <% end %>
</div>

    
asked by anonymous 30.03.2016 / 20:10

1 answer

0
accepts_nested_attributes_for :contacts, reject_if: lambda {|attributes| attributes['kind'].blank?}

In your view and customer_params I did not find the kind attribute that accepts_nested_attributes_for is not denying?

no CustomerController.rb puts a binding.pry

binding.pry 
@customer.valid?
@customer.contacts # verifica se está vazio.
@customer.contacts.errors
@customer.errors
if @customer.save

gem pry

    
10.08.2016 / 02:34