Take the tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Whenever I have used paperclip to upload multiple images I have created a separate model called images for example and used the accepts_nested_attributes_for in the associated model..

But what if i want to add multiple images without using this. So for example if my model looks like this

class Portfolio < ActiveRecord::Base
  belongs_to :sector

  attr_accessible :overview, :title, :sector_id, :photo
  has_attached_file :photo, :styles => { :portfolio => "680x680#"}
end

And my form looks like this

<%= form_for @portfolio do |f| %>

    <%= f.label :title, "Title", :class => 'title_label' %>
    <%= f.text_field :title %>

    <%= f.label :sector_id, "Choose Sector", :class => 'title_label' %><br>
    <%= f.collection_select(:sector_id, Sector.all, :id, :name, :prompt => "Please Select a Sector") %><br>

    <%= f.label :overview, "Overview", :class => 'title_label'  %>
    <%= f.text_area :overview %><br>

    <%= f.file_field :photo %><br><br>


    <%= f.submit 'Submit', :class => 'btn' %>

    <% end %>

How would i go about uploading multiple images in this scenario.. Normally within nested form for example i would use the link_to_add helper provided.

I don't have this available though with this scenario so what can i do?

is it best practive to always keep the attachments/photos separate and use nested_attributes?

Any help appreciated

share|improve this question
add comment

1 Answer

To add multiple photos you need to mention, :html => { :multipart => true} in form tag as,

<%= form_for(@portfolio, :html => { :multipart => true}) do |f| %>

and in file_tag as,

<%= f.file_field :photo, as: :file, multiple: true, name: 'photo[photo]' %>

name photo[photo] so that each photo will get same name and can easily accessed in controller. Just dtas it..

NOTE: i consider you are using jquery with paperclip to upload photos. If not try out this n let me knw if it works.. ;)

share|improve this answer
 
thanks for the answer, however how do i actually add another image once ive selected one.. –  Richlewis Aug 5 at 13:07
 
yes jquery is installed, i guess i need to create something like the link_to_add helper but unsure how to go about it –  Richlewis Aug 5 at 13:11
 
you select image collectivly like we add files in mail and click submit.. –  bunty Aug 5 at 13:11
 
oh i see, not selecting an image then selecting another separately –  Richlewis Aug 5 at 13:12
 
got it, thanks for your help –  Richlewis Aug 5 at 13:13
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.