Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing nested model attributes inside a fields_for without using FormBuilder

I have a Rails form that is being used for creating and editing a model with a has_many relationship. I'm very familiar with typical forms with nested models, but my current problem requires accessing rendering some form elements using typical FormBuilder helpers and other HTML elements using data from the model itself. For example, my top level form has something like:

<% customer_form.fields_for :customer_images do |images_form| %>
    <%= render :partial => 'customer_image_show', :locals => { :f => images_form } %>
<% end %>

Then, in the form partial, I need to do something like:

<dd><%= f.text_field :image_description %></dd>

... but also access attributes from the customer_images model (for example, the ID of the customer_image record).

I feel like this should be pretty straightforward and I'm just missing something basic. Any help is appreciated. This is a Rails 2.3.8 application.

like image 249
Chris Hart Avatar asked Nov 03 '10 02:11

Chris Hart


1 Answers

You can call

f.object

to get to the object that the form is associated with.

like image 181
monocle Avatar answered Oct 19 '22 23:10

monocle