Skip to content

Instantly share code, notes, and snippets.

@skorfmann
Created March 24, 2010 10:15
Show Gist options
  • Save skorfmann/342155 to your computer and use it in GitHub Desktop.
Save skorfmann/342155 to your computer and use it in GitHub Desktop.
<%
allowed_extensions = [:jpg, :jpeg]
max_size = 10.megabyte
allow_multiple_files = true
button_label = t('uploadify.choose')
url = url_for(:slideshow_id => slideshow, :action => :create, :controller => :slides)
%>
<%- session_key_name = ActionController::Base.session_options[:key] -%>
<% content_for :header do -%>
<%= javascript_include_tag "swfobject" %>
<%= javascript_include_tag "jquery.uploadify.v2.1.0" %>
<%= stylesheet_link_tag "uploadify"%>
<% end -%>
<% form_for(:slide, :id => 'slide_upload', :url => url, :html => { :multipart => true, "data-format" => "json", "data-allow-multiple-files" => allow_multiple_files}) do |f| %>
<%= hidden_field_tag :session_key_value, (u cookies[session_key_name]) -%>
<%= hidden_field_tag :session_key_name, ActionController::Base.session_options[:key] -%>
<%= f.error_messages %>
<%= f.file_field :image, "data-allowed-extensions" => allowed_extensions.join(", "), "data-max-file-size" => max_size, "title" => button_label %>
<% end %>
%w(middleware).each do |dir|
config.load_paths << "#{RAILS_ROOT}/app/#{dir}"
end
# config/initializer/session_store.rb
# Add this line at the end of the file
ActionController::Dispatcher.middleware.insert_before(ActionController::Session::CookieStore, FlashSessionCookieMiddleware, ActionController::Base.session_options[:key])
$('#slides').append('<%= escape_javascript(render(:partial => "slides/slide", :object => @slide)) %>').trigger("update");
$(document).ready(function(){
$("form[data-format=json]").has(":file").each(function(){
var val,
form = $(this),
input = form.find(":file");
val = function(name){
return encodeURIComponent(
form.find("input[name=" + name + "]").val()
);
};
scriptData = {
format: form.attr("data-format"),
authenticity_token: encodeURIComponent(val("authenticity_token"))
};
scriptData[val("session_key_name")] = val("session_key_value");
var options = {
uploader: '/swf/uploadify.swf',
cancelImg: '/images/cancel.png',
script: form.attr("action"),
multi: form.attr("data-allow-multiple-files"),
fileDataName: input.attr("name"),
buttonText: input.attr("title"),
fileExt: input.attr("data-allowed-extensions"),
sizeLimit: input.attr("data-max-file-size"),
scriptData: scriptData,
onComplete: function(event, queue, file, response) {
var data = eval('(' + response + ')');
if (data.result == "error"){
alert(data.error);
} else {
$.getScript(data.slide);
}
},
onSelectOnce: function(){
input.uploadifyUpload();
},
onSelect: function(event, queue, file){
if (file.size > options.sizeLimit) {
alert('Das Bild' + file.name + ' ist zu groß.');
return false;
}
}
};
input.uploadify(options);
});
});
class UploadController < InheritedResources::Base
def show
show! do |format|
format.js {render :layout => false, :format => :js }
format.json { render :json => @slide}
end
end
def update
update! do |format|
format.json {render :nothing => true }
end
end
def create
create! do |success, failure|
success.json { render :json => { :result => 'success', :slide => url_for(:slideshow_id => @slideshow, :action => "show", :controller => "slides", :id => @slide, :format => :js) } }
failure.json { render :json => { :result => 'error', :error => @slide.errors.full_messages.to_sentence } }
end
end
def destroy
super do |format|
format.html { redirect_to url_for(:id => @slideshow, :action => :show, :controller => :slideshows) }
end
end
end
/*
Uploadify v2.1.0
Release Date: August 24, 2009
Copyright (c) 2009 Ronnie Garcia, Travis Nickels
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
.uploadifyQueueItem {
font: 11px Verdana, Geneva, sans-serif;
background-color: #E5E5E5;
width: 638px;
margin-bottom: 1px;
position: relative;
height: 20px;
}
.uploadifyError {
border: 2px solid #FBCBBC !important;
background-color: #FDE5DD !important;
}
.uploadifyQueueItem .cancel {
position: absolute;
right: 2px;
top: 2px;
}
.fileName {
line-height: 18px;
margin: 1px 2px 0 4px;
}
.fileName, .percentage {
position: absolute;
z-index: 2;
}
.uploadifyProgress {
top: 0px;
left: 0px;
position: absolute;
width: 616px;
height: 20px;
}
.uploadifyProgressBar {
background-color: #0099FF;
width: 1px;
height: 20px;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment