Created
August 8, 2012 16:43
-
-
Save thomasv314/3296530 to your computer and use it in GitHub Desktop.
Multi File Upload with Rails 3 & PaperClip & jQuery
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ArtistsController < ApplicationController | |
def create_bid | |
#this is where we delete the images array so that we can | |
#add them to the model separately afterwards | |
bids = params[:art_bid].delete(:bid_images) | |
@artist = Artist.find(params[:id]) | |
@art_bid = ArtBid.new(params[:art_bid]) | |
@art_bid.artist = @artist | |
@art_bid.user = current_user | |
if @art_bid.save | |
bids[:image].each do |i| | |
image = @art_bid.bid_images.new | |
image.image = i | |
image.save | |
end | |
redirect_to @artist, :notice => "Awesome! #{@artist.name} has recieved your bid and will be in touch soon!" | |
else | |
render :text => "There was a problem :''''(" | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div class="row"> | |
<div class="twelve columns"> | |
<h1>Commission <%= @artist.name %> for work!</h1> | |
<%= form_for @art_bid, :url => bid_artist_path(@artist), :html => { :id => "new-bid-form", :multipart => true } do |f| %> | |
<fieldset> | |
<div class="row"> | |
<div class="six columns"> | |
<div id="image-upload-list"> | |
<%= f.fields_for :bid_images do |b| %> | |
<%= b.file_field :image, :class => "image-upload-field" %><br/> | |
<% end %> | |
</div> | |
</div> | |
<div class="six columns"> | |
<a class="button secondary radius" id="add-another-file">Upload Another Image</a> | |
<br/> | |
<br/> | |
</div> | |
</div> | |
</fieldset> | |
<%= f.submit :class => "button success" %> | |
<% end %> | |
</div> | |
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Image < ActiveRecord::Base | |
has_attached_file :image | |
end | |
class ArtBid | |
has_many :bid_images, :class => :image | |
end | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(document).ready(function() { | |
var image_field = $('.image-upload-field'); | |
var image_field_name = $(image_field).attr('name'); | |
$(image_field).attr('name', image_field_name+"[]"); | |
var addAnotherFile = function(evt) { | |
evt.preventDefault(); | |
console.log("Need to add another one of these:"); | |
var new_image_field = $(image_field).clone(); | |
var new_image_name = image_field_name + "[]"; | |
$(new_image_field).attr('name', new_image_name); | |
$('<br/>').prependTo('#image-upload-list'); | |
$(new_image_field).prependTo('#image-upload-list'); | |
} | |
$('#add-another-file').bind('click', addAnotherFile); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment