Marko Anastasov wrote this on February 25, 2009
Getting model instances from form objects
What happens when you you’re using the new nested attributes feature from the upcoming Rails 2.3, with code in your view like
<% main_model_form.fields_for :nested_things do |nt_form| %># ...
and you don’t want just to render form fields for these, but to access one of the current nestedthing’s attributes? For example, we don’t want to display the filefield, since leaving it blank would make Paperclip think that the associated attachment should be deleted, but render a link to the attachment instead.
fieldsfor “creates a scope around a specific model object like formfor” doc, and one of the attributes that you can access is called objectname. Say we’re in an edit action’s view. objectname then is a string which looks like main_model[nestedthingattributes][id]. So in order to extract that id number and get the right object instance, you can do
Update: this is all it really takes -
Thanks to Mark for reminding us to update the post.