Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

fix file upload

  • Loading branch information...
commit 1859c20c3bd13834b6863fbdcab99abaf74da3e0 1 parent 649bded
@jodyalbritton authored
View
3  Gemfile
@@ -43,6 +43,7 @@ gem 'canonical-rails', github: 'jumph4x/canonical-rails'
gem 'jquery-datatables-rails', '~> 2.1.10.0.2'
+
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'
@@ -55,4 +56,4 @@ gem 'jquery-datatables-rails', '~> 2.1.10.0.2'
# Use debugger
# gem 'debugger', group: [:development, :test]
-gem 'jquery.fileupload-rails'
+gem 'jquery-fileupload-rails'
View
16 Gemfile.lock
@@ -42,7 +42,7 @@ GEM
authority (3.0.0)
activesupport (>= 3.0.0)
rake (>= 0.8.7)
- autoprefixer-rails (2.1.0.20140628)
+ autoprefixer-rails (2.1.1.20140710)
execjs
awesome_nested_set (3.0.0.rc.6)
activerecord (>= 4.0.0, < 5)
@@ -89,23 +89,21 @@ GEM
hashie (3.2.0)
hike (1.2.3)
i18n (0.6.11)
- jbuilder (2.1.2)
+ jbuilder (2.1.3)
activesupport (>= 3.0.0, < 5)
multi_json (~> 1.2)
jquery-datatables-rails (2.1.10.0.3)
jquery-rails
sass-rails
+ jquery-fileupload-rails (0.4.1)
+ actionpack (>= 3.1)
+ railties (>= 3.1)
jquery-rails (3.1.1)
railties (>= 3.0, < 5.0)
thor (>= 0.14, < 2.0)
jquery-turbolinks (2.0.2)
railties (>= 3.1.0)
turbolinks
- jquery-ui-rails (4.2.1)
- railties (>= 3.2.16)
- jquery.fileupload-rails (1.8.1)
- jquery-ui-rails (>= 3.0, < 5.0)
- railties (>= 3.1)
json (1.8.1)
libv8 (3.16.14.3)
mail (2.5.4)
@@ -185,7 +183,7 @@ GEM
coffee-rails
tzinfo (1.2.1)
thread_safe (~> 0.1)
- uglifier (2.5.1)
+ uglifier (2.5.3)
execjs (>= 0.3.0)
json (>= 1.8.0)
warden (1.2.3)
@@ -209,9 +207,9 @@ DEPENDENCIES
gmaps4rails
jbuilder (~> 2.0)
jquery-datatables-rails (~> 2.1.10.0.2)
+ jquery-fileupload-rails
jquery-rails
jquery-turbolinks
- jquery.fileupload-rails
mysql2
paperclip (~> 4.1)
rails (= 4.1.2)
View
6 app/assets/javascripts/application.js
@@ -14,10 +14,11 @@
//= require jquery.turbolinks
//= require jquery_ujs
//= require bootstrap-sprockets
-//= require jquery.fileupload
+//= require jquery-fileupload/basic
+//= require jquery-fileupload/vendor/tmpl
//= require dataTables/jquery.dataTables
//= require dataTables/bootstrap/3/jquery.dataTables.bootstrap
-//= require underscore
+//= require attachments
//= require gmaps/google
//= require turbolinks
@@ -35,4 +36,3 @@ ready = function() {
};
-$(document).on('page:change', ready);
View
23 app/assets/javascripts/attachments.js.coffee
@@ -2,3 +2,26 @@
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/
+jQuery ->
+ $('#attachment_photo').attr('name','attachment[photo]')
+ $('#new_attachment').fileupload
+ dataType: 'script'
+ add: (e, data) ->
+ types = /(\.|\/)(gif|jpe?g|png|mov|mpeg|mpeg4|avi)$/i
+ file = data.files[0]
+ if types.test(file.type) || types.test(file.name)
+ data.context = $(tmpl("template-upload", file))
+ $('#new_attachment').append(data.context)
+ $('.actions input[type="submit"]').click (e) ->
+ $('.actions input[type="submit"]').attr("disabled", true)
+ data.submit()
+ e.preventDefault()
+ else
+ alert("#{file.name} is not a gif, jpg or png image file")
+ progress: (e, data) ->
+ if data.context
+ progress = parseInt(data.loaded / data.total * 100, 10)
+ data.context.find('.bar').css('width', progress + '%')
+ done: (e, data) ->
+ $('.actions input[type="submit"]').off('click')
+ $('.actions input[type="submit"]').attr("enabled", true)
View
26 app/controllers/attachments_controller.rb
@@ -10,25 +10,21 @@ def new
end
def create
-
-
- @cemetery = Cemetery.friendly.find(params[:cemetery_id])
- @plot = Plot.friendly.find(params[:plot_id])
- @attachment = Attachment.new(attachment_params)
- @attachment.imageable_id = params[:imageable_id]
- @attachment.imageable_type = params[:imageable_type]
- @attachment.save!
-
-
-
+ if params[:plot_id]
- respond_to do |format|
+ @plot = Plot.friendly.find(params[:plot_id])
+ @cemetery = Cemetery.find(@plot.cemetery)
+ @attachment = @plot.attachments.create(attachment_params)
+ else
+ @attachment = Attachment.create(attachment_params)
+ end
+ respond_to do |format|
if @attachment.save
- format.html { redirect_to cemetery_plot_path(@cemetery, @plot), notice: 'Attachment was successfully created.' }
- format.json { render :show, status: :created, location: @attachment }
+ format.html { redirect_to cemetery_plot_path(@cemetery, @plot), notice: 'Photo was successfully created.' }
+ format.json { render :show, status: :created, location: @plot }
else
format.html { render :new }
- format.json { render json: @attachment.errors, status: :unprocessable_entity }
+ format.json { render json: @plot.errors, status: :unprocessable_entity }
end
end
end
View
64 app/views/attachments/_form.html.erb
@@ -1,46 +1,28 @@
-<div class="container">
- <h2>Upload file</h2>
- <%= form_for ([@cemetery, @plot, @attachment]), :html => { :multipart => true, :id => "fileupload"} do |f| %>
- <!-- The fileupload-buttonbar contains buttons to add/delete files and start/cancel the upload -->
- <div class="row fileupload-buttonbar">
- <div class="span7">
- <!-- The fileinput-button span is used to style the file input field as button -->
- <span class="btn btn-success fileinput-button">
- <i class="icon-plus icon-white"></i>
- <span>Add files...</span>
- <%= f.file_field :photo %>
- <%= hidden_field_tag :imageable_id, @plot.id %>
- <%= hidden_field_tag :imageable_type, @plot.class %>
+<% @attachment = @plot.attachments.new %>
+ <%= form_for ([@cemetery, @plot, @attachment]), :class=> "form-horizontal panel", :html => { :multipart => true} do |f| %>
+ <div class="panel-body">
+ <% if @attachment.errors.any? %>
+ <div id="error_explanation">
+ <h2><%= pluralize(@attachment.errors.count, "error") %> prohibited this attachment from being saved:</h2>
- </span>
- <button type="submit" class="btn btn-primary start">
- <i class="icon-upload icon-white"></i>
- <span>Start upload</span>
- </button>
- <button type="reset" class="btn btn-warning cancel">
- <i class="icon-ban-circle icon-white"></i>
- <span>Cancel upload</span>
- </button>
- <button type="button" class="btn btn-danger delete">
- <i class="icon-trash icon-white"></i>
- <span>Delete</span>
- </button>
- <input type="checkbox" class="toggle">
- </div>
- <div class="span5">
- <!-- The global progress bar -->
- <div class="progress progress-success progress-striped active fade">
- <div class="bar" style="width:0%;"></div>
- </div>
- </div>
+ <ul>
+ <% @attachment.errors.full_messages.each do |msg| %>
+ <li><%= msg %></li>
+ <% end %>
+ </ul>
</div>
- <!-- The loading indicator is shown during image processing -->
- <div class="fileupload-loading"></div>
- <br>
- <!-- The table listing the files available for upload/download -->
- <table class="table table-striped"><tbody class="files" data-toggle="modal-gallery" data-target="#modal-gallery"></tbody>
- </table>
<% end %>
-</div>
+
+
+ <%= f.file_field :photo, multiple: true, name: 'attachment[photo]', :hint=> "You can select multiple files" %>
+
+ </div>
+ <div class="panel-footer text-right actions">
+ <%= f.submit "save", class: 'btn' %>
+ </div>
+<% end %>
+
+<% # jquery upload template # %>
+<script id="template-upload" type="text/x-tmpl"><div class="upload">{%=o.name%}<div class="progress"><div class="progress-bar bar" style="width: 0%"></div></div></div></script>
View
1  app/views/attachments/_upload.html.erb
@@ -0,0 +1 @@
+<%= image_tag(@attachment.photo(:small), class: 'thumb') %>
View
5 app/views/attachments/create.js.erb
@@ -0,0 +1,5 @@
+<% if @attachment.new_record? %>
+ alert('Failed');
+<% else %>
+ $('ul.thumbnails').append("<%=j render partial: 'attachments/upload', locals: { attachment: @attachment } %>");
+<% end %>
View
27 app/views/plots/show.html.erb
@@ -93,11 +93,7 @@
<div class="panel panel-default">
<div class="panel-heading">
- <%= link_to new_cemetery_plot_attachment_path(@cemetery, @plot), :class=> "btn btn-primary pull-right" do %>
-
- <i class="glyphicon glyphicon-camera"></i>
- <span><strong>Add Photo</strong></span>
- <% end %>
+ <button class="btn btn-primary btn-xs pull-right" data-toggle="modal" data-target="#newAttachment">Add Attachments</button>
<h4>Photos</h4>
</div>
<div class="panel-body">
@@ -153,7 +149,28 @@
+<!-- 5. $DEFAULT_MODAL =============================================================================
+ Default modal
+-->
+ <!-- Modal -->
+ <div id="newAttachment" class="modal fade" tabindex="-1" role="dialog" style="display: none;">
+ <div class="modal-dialog">
+ <div class="modal-content">
+ <div class="modal-header">
+ <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
+ <h4 class="modal-title" id="myModalLabel">New Photo</h4>
+ </div>
+ <div class="modal-body">
+ <%= render "attachments/form" %>
+ </div> <!-- / .modal-body -->
+ </div> <!-- / .modal-content -->
+ </div> <!-- / .modal-dialog -->
+ </div> <!-- /.modal -->
+ <!-- / Modal -->
+
+
+<!-- /5. $DEFAULT_MODAL -->
Please sign in to comment.
Something went wrong with that request. Please try again.