Skip to content

Instantly share code, notes, and snippets.

@cemeng
Created June 21, 2012 11:46
Show Gist options
  • Save cemeng/2965288 to your computer and use it in GitHub Desktop.
Save cemeng/2965288 to your computer and use it in GitHub Desktop.
Nested Attributes For
In the klass
class JobInvoice < SearchableRecord
.. yada yada yada ...
accepts_nested_attributes_for :job_invoice_items
end
In the view
<%= form_for @job_invoice do |f| %>
....
<%= f.fields_for :job_invoice_items do |builder| %>
<tr>
<td class="line_no">1</td>
<td><%= builder.text_field :description %></td>
<td class="qty"><%= builder.text_field :quantity %></td>
<td class="unit_price"><%= builder.text_field :unit_price %></td>
<td class="line_price"></td>
</tr>
<% end %>
<% end %>
On submit:
Parameters: {"utf8"=>"✓", "authenticity_token"=>"lCj6XbLdj0XO0LPR6Cy+LLixjuri9nMbh6+/DB7Wpr4=", "job_no"=>"WWH11", "job_invoice"=>{"notes"=>"test", "job_invoice_items_attributes"=>{"0"=>{"description"=>"felix 23", "quantity"=>"3", "unit_price"=>"0.0", "id"=>"1"}}}, "commit"=>"Save changes", "id"=>"2892511"}
Thoughts:
- When there is no job_invoice_items -> f.fields_for doesn't render anything.. -> must be a way
- How to make it to render 10 empty rows?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment