0

I am having difficulty understanding the way to layout the view to have the properties nest properly for a nested form.

My params look like this...

Parameters: {"utf8"=>"✓",
  "authenticity_token"=>"retsty/G2I1FyYybXm9kVaZi+aPjQm4jsmLdLW3wxsc=", 
  "receipt"=>{"receipt"=>"", "receipt_date(1i)"=>"2014", "receipt_date(2i)"=>"9", "receipt_date(3i)"=>"19", 
  "po_receipts_attributes"=>[
    {"jobquote_order_id"=>{"3"=>"", "5"=>""}, "qty"=>"30"}, 
    {"qty"=>"11"}
  ]},
  "commit"=>"Receive"}

The po_receipts_attributes array is not nesting the hash correctly.

Format: po_receipts_attributes: [{jobquote_order_id: <value>, qty: <value>}, {jobquote_order_id: <value>, qty: <value>}]

According to http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html, my params should look like this...

Parameters: {"utf8"=>"✓",
  "authenticity_token"=>"retsty/G2I1FyYybXm9kVaZi+aPjQm4jsmLdLW3wxsc=", 
  "receipt"=>{"receipt"=>"", "receipt_date(1i)"=>"2014", "receipt_date(2i)"=>"9", "receipt_date(3i)"=>"19", 
  "po_receipts_attributes"=>[
    {"jobquote_order_id"=>"3", "qty"=>"30"},
    {"jobquote_order_id"=>"5", "qty"=>"11"}
  ]},
  "commit"=>"Receive"}

I can't seem to get the way to nest an attributes correctly in the view. I have tried many different syntaxes for this.

In my view I have the following code...

= hidden_field "receipt[po_receipts_attributes][][jobquote_order_id]", jq_o.id
= text_field_tag "receipt[po_receipts_attributes][][qty]", ""

HTML output...

<td>
  <input id="receipt_po_receipts_attributes__jobquote_order_id_3" name="receipt[po_receipts_attributes][][jobquote_order_id][3]" type="hidden" />
  <input id="receipt_po_receipts_attributes__qty" name="receipt[po_receipts_attributes][][qty]" type="text" value="" />
</td>
<td>
  <input id="receipt_po_receipts_attributes__jobquote_order_id_5" name="receipt[po_receipts_attributes][][jobquote_order_id][5]" type="hidden" />
  <input id="receipt_po_receipts_attributes__qty" name="receipt[po_receipts_attributes][][qty]" type="text" value="" />
</td>

What would be the best way to handle this?

3 Answers 3

3

It should be something like this:

jobquote_orders.each_with_index do |jq_o, i|
  = hidden_field_tag "recepit[po_receipts_attributes][#{i}][jobquote_order_id]", jq_o.id
  = text_field_tag "receipt[po_receipts_attributes][#{i}][qty]", ""

Which should generate params:

"po_receipts_attributes" => {
  "0" => {"jobquote_order_id"=>"3", "qty"=>"30"},
  "1" => {"jobquote_order_id"=>"5", "qty"=>"11"}
}

Which you can later, save(manually as you specified in your question):

params[:receipt][:po_receipts_attributes].values.each do |v|
  pr = PoReceipt.find(v['jobquote_order_id'].to_s)
  pr.update_atributes(v.except('id'))
end
Sign up to request clarification or add additional context in comments.

6 Comments

Thank you for a solution. Would you be able to allow the accepts_nested_attributes_for to handle it automatically with that syntax? I thought about using an index like that, but since it didn't follow the API docs, it seems incorrect. I needed to create the view manually (I think) due to the recordset that was returned, or I would have used fields_for.
Yes, you can I presume. If you are using accept_nested_attributes then why are you setting them manually at all?
"due to the recordset that was returned". I need to test the order to see if it is closed prior to it being included in the form.
It still isn't formatted correctly. This is what was returned when I added the index to the []... "po_receipts_attributes"=>{"0"=>{"jobquote_order_id"=>{"3"=>""}, "qty"=>""}, "1"=>{"jobquote_order_id"=>{"5"=>""}, "qty"=>""}}
@Beengie see the updated answer. You need to change hidden_field to hiddel_field_tag and then it'll work. Sorry for the inconvenience.
|
1

You should have an integer inside the inner [] like:

= hidden_field "recepit[po_receipts_attributes][1][jobquote_order_id]", jq_o.id
= text_field_tag "receipt[po_receipts_attributes][1][qty]", ""

That integer is what keeps the attributes together when Rails parses the form body into the params hash. Each set of po_receipt_attributes will need a distinct ID. If you are generating the fields server side you can use fields_for and the IDs will be managed for you automatically.

2 Comments

http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html shows the attributes array the way I was targeting. Before I asked the question here I tried putting the jq_o.id in the [] as you have stated, but I didn't see that following the API docs. I'm confused.
When I have the jq_o.id placed in the [], I get... "po_receipts_attributes"=>{"3"=>{"jobquote_order_id"=>{"3"=>""}, "qty"=>"30"}, "5"=>{"jobquote_order_id"=>{"5"=>""}, "qty"=>"11"}}
0

Try to use fields_for as the following:

<%= form_for @receipt do |receipt_form| %>
  <%= receipt_form.fields_for :po_receipts do |po_receipts_fields| %>
    <%= po_receipts_fields.hidden_field :jobquote_order_id, jq_o.id %>
    <%= po_receipts_fields.text_field :qty %>
  <% end %>
<% end %>

1 Comment

Thanks Mohamed. The reason why I went this route is due to the fact that I am pulling data from another table into the mix which I have not listed here. jobquote_orders is a many-to-many table and it is driving the number of potential po_receipts for open orders.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.