Skip to content

Instantly share code, notes, and snippets.

@zorn
Created November 22, 2011 02:46
Show Gist options
  • Save zorn/1384743 to your computer and use it in GitHub Desktop.
Save zorn/1384743 to your computer and use it in GitHub Desktop.
# _form.html.erb
<%= f.fields_for :file_attachments do |builder| %>
<%= render 'file_attachment_fields', :f => builder %>
<% end %>
# _file_attachment_fields.html.erb
<div class="clearfix fields">
<label>Name:</label>
<div class="input">
<%= f.text_field :name %>
<%= f.file_field :attachment %>
<%= f.hidden_field :_destroy %>
<%= link_to_function "Remove", "remove_fields(this)" %>
</div>
</div>
# So what I'd like to do is make the partial something like:
<div class="clearfix fields">
<label>Name:</label>
<div class="input">
<%= f.text_field :name %>
<% if @file_attachment.new? %>
<%= f.file_field :attachment %>
<% else %>
<a href="<%= @file_attachment.attachment.url %>"><%= @file_attachment.attachment_file_name %></a>
<% end %>
<%= f.hidden_field :_destroy %>
<%= link_to_function "Remove", "remove_fields(this)" %>
</div>
</div>
# But using fields_for and passing in a collection
# I'm unsure how I can access the current object
# (ala @file_attachment) in the iteration.
@r38y
Copy link

r38y commented Nov 22, 2011

and f.object doesn't work?

@zorn
Copy link
Author

zorn commented Nov 22, 2011

I did not know about f.object. Seems to work. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment