Wednesday 20 January 2010

Getting the id of the object when inside form_for or fields_for

This was a post I put on StackOverflow - and then found the answer about 20 mins later ... doh

I have the following code that is generating fields for an invoice

THis is in the edit.html.erb for the invoice class

<% f.fields_for(:invoice_items) do |f| %>
<%= render :partial => 'invoice_items/fields', :locals => {:f => f} %>
<% end %>


and I generate the invoice_items as part of the invoice object

@invoice = Invoice.find(params[:id], :include => :invoice_items, :order =>"invoice_items.display_order")

It works just fine, but I need to wrap each one in a div, and assign that object's id to the div. (div id=i_2345 - that kind of thing) so I can use jQuery wizardry.

Where I am stumbling like a new-born foal is how do I access the the id of the invoice_item that is being called?

And the answer is ...

f.object.id

No comments:

Post a Comment