I have the following associations:
class Developer < ActiveRecord::Base
has_many :large_photos, :class_name => 'Photo', :conditions => { :large => true }, :reject_if => proc { |x| x['image'].blank? }
end
class Photo < ActiveRecord::Base
belongs_to :developer
mount_uploader :image, ImageUploader # Carrierwave
end
Now, I'm looking to build a Development form which, when submitted, will add a single file field for uploading a new Photo
. At the moment I have:
<%= form_for @developer do |form| %>
<%= form.fields_for :large_photos do |sf| %>
<div class="dropzone">
<%= sf.file_field :image %>
<%= sf.hidden_field :large, :value => '1' %>
<%= sf.hidden_field :image_cache %>
</div>
<% end %>
<% end%>
Now, the problem I have with this is that fields_for
is looping through the existing records (of course it is!) which I'd prefer to avoid, I just want to add a single file field for uploading a new photo. How would I go about this?
<%= form.fields_for :large_photos, @developer.large_photos.build do |project_fields| %>
Instead of creating the fields for each existing photos, this line builds a new Photo
and show the fields only for this one (so the fields are empty, I think it's what you want).