Take the 2-minute tour ×

Currently I have a has_one relationship betweeen Users and photos.

User model:
has_one :photo
accepts_nested_attributes_for :photo
Photo model:
  belongs_to :user
  has_attached_file :image,
          :path => ':rails_root/public/images/ads/:id/:basename.:extension',
          :url  => "images/ads/:id/:basename.:extension"

The nested form:

 <%= f.simple_fields_for :photo_attributes, :html => { :multipart => true } do |d| %>
    <%= d.input :billed_navn %>
    <%= d.label :image, :label => 'Upload logo', :required => false  %>
    <%= d.file_field :image, :label => 'Image', :class => 'imagec', :required => 'false', :style => 'margin-bottom:2px;float:left;width:250px;'  %>
    <input type="button" value="Clear" id="clear" style="width:70px;float:left;margin-right:2px;">
    <%= d.input :image_url, :label => 'Billed URL', :input_html => { :class => 'imagec'}, :required => false %>
    <%= f.label :image, :label => 'Billed preview', :required => false  %><div id="preview"></div>
<% end %>

This setup works as it should, I can upload 1 photo. I users to be able to upload multiple photos at once.

Therefor I have changed the assocition in useres model to:

User model:
has_many :photos
accepts_nested_attributes_for :photos

But I how should the nested form then be? If it should be possible to upload mulitple images at once?

share|improve this question
you have to build photos like user.photos.build or user.build_photos and f.fields_for :photos,you can refer railscasts.com/episodes/196-nested-model-form-part-1 –  Amar Sep 1 '12 at 10:41
add comment

1 Answer

up vote 0 down vote accepted

The accepts_nested_attributes_for thing only allows mass assignment to add multiple photos at once. (Beware of mass assignment security vulnerabilities! strong_parameters gem recommended). This means that the update action accepts multiple photos.

It will only add it if they are sent, which happens if there are fields in the form that a user fills out. This is mainly determined by the edit view.

Because you don't know how many photos a user will want to add, the best way to do this is to use javascript to dynamically add an extra set of fields for a photo when requested by the user. This can be a link, which when clicked, appends the fields to the form. This way the user can submit as many photos at once as they want.

You will also want to have some validation so that if a set of empty fields (for a photo) are submitted, it doesn't add a non-photo photo.

If you don't want to use javascript, the best you can do is to just assume the user will upload at most say 3 at a time, and include 3 sets of photo fields. Again, being careful to deal with empty fields appropriately.


<% (1..5).each do |I| %>
  <%= fields_for "user[photo_attributes][]", nil, :index => I do |form| %>
    <%= form.input :billed_navn %>
  <% end %>
<% end %>
share|improve this answer
yeah, but how should the form be and the model? :O –  Rails beginner Sep 1 '12 at 15:42
You just need to have multiple copies of the fields, passing a different :index option each time - see stephenchu.com/2008/03/… for doing this statically. For a step-by-step guide to nested forms, look at railscasts.com/episodes/… and railscasts.com/episodes/… for dynamic forms. –  ronalchn Sep 1 '12 at 23:02
add comment

Your Answer


