Skip to content

Instantly share code, notes, and snippets.

@Mirai
Created August 9, 2012 16:23
Show Gist options
  • Save Mirai/3305614 to your computer and use it in GitHub Desktop.
Save Mirai/3305614 to your computer and use it in GitHub Desktop.
Nested fields_for
_form.html.erb
<% form_for @estimate, :url => estimates_path do |f| %>
<div id="estimate_items">
<%= render :partial => "job_part", :collection => @estimate.job_parts %>
</div>
<%= add_job_part "Add Calendar" %>
<p><%= f.submit "ADD CALENDAR TO ESTIMATE" %></p>
<% end %>
---
_job_part.html.erb
<div class="item">
<% fields_for "estimate[job_part_attributes][]", job_part do |jp| %>
<div style="float: left">
<p>
<label>Calendar Type</label>
<%= jp.select :product_id, Product.find_all_by_store_front_id(StoreFront.current).collect {|p| [p.name, p.id]} %>
</p>
<p>
<label>Quantity</label>
<%= jp.text_field :quantity %>
</p>
</div>
<div style="float: left">
<h3>Ship to Location</h3>
<% jp.fields_for "address_attributes" do |a| %>
<p>
<label>Last Name</label>
<%= a.text_field :last_name" %>
</p>
<p>
<label>Address 1<label>
<%= a.text_field :address_line_1 %>
</p>
<p>
<label>Address 2</label>
<%= a.text_field :address_line_2 %>
</p>
<p>
<label>City</label>
<%= a.text_field :city %>
<p>
<label>State</label>
<%= a.text_field :state_province_region %>
<label>Zip</label>
<%= a.text_field :zip_postal_code %>
</p>
<p>
<label>Country</label>
<%= a.text_field :country %>
</p>
<% end %>
</div>
<%= link_to_function "Remove", "$(this).parents('.item').remove()" %>
<% end %>
</div>
---
<form action="/estimates" class="new_estimate" id="new_estimate" method="post">
<div id="estimate_items">
<div class="item">
<div style="float: left">
<p>
<label>Calendar Type</label>
<select id="estimate_job_part_attributes__product_id" name="estimate[job_part_attributes][][product_id]"></select>
</p>
<p>
<label>Quantity</label>
<input id="estimate_job_part_attributes__quantity" name="estimate[job_part_attributes][][quantity]" size="30" type="text" />
</p>
</div>
<div style="float: left">
<h3>Ship to Location</h3>
<p>
<label>Last Name</label>
<input id="estimate_job_part_attributes__address_attributes_last_name" name="estimate[job_part_attributes][][address_attributes][last_name]" size="30" type="text" />
</p>
<p>
<label>Address 1<label>
<input id="estimate_job_part_attributes__address_attributes_address_line_1" name="estimate[job_part_attributes][][address_attributes][address_line_1]" size="30" type="text" />
</p>
<p>
<label>Address 2</label>
<input id="estimate_job_part_attributes__address_attributes_address_line_2" name="estimate[job_part_attributes][][address_attributes][address_line_2]" size="30" type="text" />
</p>
<p>
<label>City</label>
<input id="estimate_job_part_attributes__address_attributes_city" name="estimate[job_part_attributes][][address_attributes][city]" size="30" type="text" />
<p>
<label>State</label>
<input id="estimate_job_part_attributes__address_attributes_state_province_region" name="estimate[job_part_attributes][][address_attributes][state_province_region]" size="30" type="text" />
<label>Zip</label>
<input id="estimate_job_part_attributes__address_attributes_zip_postal_code" name="estimate[job_part_attributes][][address_attributes][zip_postal_code]" size="30" type="text" />
</p>
<p>
<label>Country</label>
<input id="estimate_job_part_attributes__address_attributes_country" name="estimate[job_part_attributes][][address_attributes][country]" size="30" type="text" />
</p>
</div>
<a href="#" onclick="$(this).parents('.item').remove(); return false;">Remove</a>
</div>
</div>
<p><input id="estimate_submit" name="commit" type="submit" value="ADD CALENDAR TO ESTIMATE" /></p>
</form>
---
{"commit"=>"ADD CALENDAR TO ESTIMATE",
"authenticity_token"=>"a9d039735e7a26e7b3fb1499dde7781fb8314518",
"estimate"=>{"job_part_attributes"=>[{"product_id"=>"2039",
"quantity"=>"123",
"address_attributes"=>{}},
{"address_attributes"=>{"address_line_1"=>"285 Settler Rd"}},
{"address_attributes"=>{"address_line_2"=>""}},
{"address_attributes"=>{"city"=>"DeKalb"}},
{"address_attributes"=>{"state_province_region"=>"Illinois"}},
{"address_attributes"=>{"zip_postal_code"=>"60115"}},
{"address_attributes"=>{"country"=>"United States"}}]}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment