How to save data from a view that is inside a view in Rails

Asked

Viewed 61 times

1

I rendered a view inside another, because my register has 3 tables inside a CRUD.

the form is in the view.

I rendered the custom_address this way,

Note: it will only save the customer’s address.

<%= form.fields_for :customer_addresses, @customer.customer_addresses.build do |customer_address_fields| %>
  <%= render 'customer_address_fields', customer_address_fields: customer_address_fields %>
<% end %>

But when I save the Lord, he’s not saving the Lord, he wanted a hand in it.

def customer_address_params
  params.permit(:address_type_id, :primary_address, :country, :zip_code, :street, :number, :complement, :district, :city, :state)
end

But no value is coming in the variables.

1 answer

2

If your code refers to StrongParameters (allowed parameters) is just as you placed above, you need to change to pick up the parameters from custumer. Probably if you look at your log server rails s, is coming something like

params = {person = {'name'=>'Costumer X', 
'costumer_address' => { 'address_type_id'=>1, 'zip_code'=>'01310100', 
'street'=>'Avenida Paulista'....}

So to get so many parameters from the costumer, when those of the costumer_address, you should allow more or less like this:

params.require(:costumer).permit(:name, 
  :other_costumer_params,
  { costumer_addresses_attributes: [:address_type_id, :primary_address, 
  :country, :zip_code, :street, :number, :complement,
  :district, :city, :state]})

Browser other questions tagged

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