Based on Railscast 196 and 197, I’ve created a nested form for adding multiple illustrations (image attachments) to my article model. I have a form like this:
<%= form_for @article, :html => { :multipart => true } do |f| %>
<h2>Illustrations</h2>
<% for illustration in @article.illustrations %>
<%= f.fields_for :illustrations, illustration do |builder| %>
<%= render :partial => "illustration_fields",
:locals => { :f => builder, :illustration => illustration } %>
<% end %>
<% end %>
<p>
<%= f.submit "Submit" %>
</p>
<% end %>
And the _illustration_fields partial looks like this:
<div class="fields">
<p>
<%= f.label :illustration, "Illustration" %><br />
<%= link_to_remove_fields "remove", f %><br />
<%= f.file_field :illustration %>
</p>
</div>
If the illustration already exists, I want to display it on the page inside an image tag. I tried displaying the image inside the partial like this:
<% unless :illustration.illustration.blank? %>
<%= image_tag(:illustration.illustration.url) %>
<% end %>
But it throws up an error:
undefined method `illustration' for :illustration:Symbol
The illustration field definitely exists in the illustration model, as I can display the image inside the main form like this:
<% unless illustration.illustration.blank? %>
<%= image_tag(illustration.illustration.url) %>
<% end %>
It seems that Rails doesn’t understand the illustration information into the partial (or I’m not passing it correctly). What is the correct approach to going about this?
Solved it. The illustration object can be accessed from inside the partial as follows. No need to pass it inside the
:localshash.