Hey there, I want to be able to upload multiple images at once on my
website. I can only upload one image at a time at the moment. Can
anybody lead me in the right direction? Here's my code
/app/models/listing.rb
class Listing < ActiveRecord::Base
has_attached_file :image, :styles => { :medium => "200x", :thumb =>
"100x100>" }, :default_url => "default.jpg"
validates_attachment_content_type :image, :content_type =>
/\Aimage\/.*\Z/
end
/app/controllers/listings_controller.rb
...
private
# Use callbacks to share common setup or constraints between actions.
def set_listing
@listing = Listing.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white
list through.
def listing_params
params.require(:listing).permit(:name, :description, :price, :image)
end
end
/app/views/listings/_form.html.erb
<%= form_for @listing, :html => { :multipart => true } do |f| %>
...
...
<div class="form-group">
<%= f.file_field :image, class: "form-control" %>
</div>
<div class="form-group">
<%= f.submit class: "btn btn-primary" %>
</div>
<% end %>
/app/views/listings/show.html.erb
<p id="notice"><%= notice %></p>
<%= image_tag @listing.image.url(:medium) %>
...
/app/views/listings/index.html.erb
<h1>Listing listings</h1>
<table>
<thead>
<tr>
<th>Image</th>
<th>Name</th>
...
<tbody>
<% @listings.each do |listing| %>
<tr>
<td><%= image_tag listing.image.url(:medium) %></td>
<td><%= listing.name %></td>
...
In order to do this (no matter which file attachment system you choose) you will need to refactor your application slightly. You need a model instance *per* image uploaded, so the usual way to do this is with a nested form. You create a form on a parent object that `has_many` of the image objects. If the Listing model is the parent, then you can add an Image model to belong to that listing, in a one-to-many relationship. Then you set up the following (this is pseudocode, off the top of my head, not guaranteed to work as written, but the right idea, IMO):
class Listing < ActiveRecord::Base
has_many :images
accepts_nested_attributes_for :images, allow_destroy: true, reject_if: :all_blank
end
class Image < ActiveRecord::Base
belongs_to :listing
has_attached_file :image
end
class ListingsController < ApplicationController
... all the usual CRUD actions here
private
def listing_params
params.require(:listing).permit(:name, :description, :price, images_attributes: [:id, :image, :_destroy])
end
end
And finally, in your form_for @listing, you need to create a very particularly named field in order to get this all to work together:
<%= file_field_tag('listing_images_attributes_image', multiple: true, :name => "listing[images_attributes][image]") %>
That last part is ripped out of working code using Paperclip.
Things to note here:
1. It's using the file_field_tag generator (long-hand) rather than the f.file_field that you may be used to. This is so that the name can be exactly what it needs to be to "trick" the accepts_nested_attributes_for helper into accepting the attached files.
2. The multiple: true thing means that you will need a modern browser to use this (HTML5 feature).
3. The empty square-brackets in the middle of the name are the "secret sauce" that cause the individual files in the multiple file form element to be sent as separate files, rather than the last one "winning" or the data being concatenated and sent as form post body instead.
4. This type of field is only useful on an initial create or upload screen, not if you want to edit each of the attached files individually. I would put this particular form on the #new screen.
Once you have this working, you will need to make your #edit screen a more traditional nested form. Read this for an example and further ideas: #196 Nested Model Form (revised) - RailsCasts
Walter