Nested Attributes Rails Child model not saved

Asked

Viewed 79 times

2

Good afternoon!

I’m having a problem with storing models 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 I put it here and decided to make it work only with Contact before. Well, at first the Customer is saved, but the Contact no. From what I read, there’s no need to repeat the .build model Child in function create, and the line "@Customer = Customer.new(customer_params)" create and save both. After searching hard for the error, I discovered that in the parameters that came from form, the hash required to register Contact came with the following structure ":contacts_attributes[:0[...(correct data)]]" that is, the data came encapsulated inside another hash. Because of this, my other solution did not work (Put the Contacts.build(customer_params[:contacts_attributes]) the more). I imagine this is in case I add more than one Contact at a time.

I couldn’t find a clear explanation of how this works, but I’ve arrived at 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>

  • Can you log the request you made along with the question? Logs often help you understand the problem.

1 answer

0


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

In your view and on customer_params i did not find the attributo Kind will be the accepts_nested_attributes_for is not denying?

in the 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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.