Hiro Hiro - 3 months ago 11
Ruby Question

Nested_attributes dont appear in my params

I'm trying to include

nested_form
in one of my model new form.

<%= form_for :master_box, url: master_boxes_path do |f| %>
<%= f.label :number %><br>
<%= f.text_field :number %>

<%= f.fields_for :orders do |o| %>
<fieldset>
<%= o.label :number, "Number" %>
<%= o.text_field :number %>
</fieldset>
<% end %>
<br>
<%= f.submit %>
<% end %>


I don't know why when I submit to create my masterbox, my params don't contain
orders_attributes
.

Here's my models :

class MasterBox < ActiveRecord::Base
has_many :orders
accepts_nested_attributes_for :orders
end


class Order < ActiveRecord::Base
has_many :products
belongs_to :master_box
end


and my strong parameters :

def master_box_params
params.require(:master_box).permit(:number, :number_orders, orders_attributes: [:number])
end


It is pretty simple but I don't understand why I don't have
orders_attributes
in my params.
I have this instead, only "orders" :
enter image description here

Answer

Try changing the first line of your form to:

<%= form_for @master_box, url: master_boxes_path do |f| %>

and also passing through the id in your params:

def master_box_params
  params.require(:master_box).permit(:number, :number_orders, orders_attributes: [:id, :number])
end