In my project I currently have different models (Project, Message, etc) that:
has_many :assets, :as => :attachable, :dependent => :destroy
Each Asset is basically a model with a CarrierWave file. Normally I would just use accepted_nested_attributes on the parent model (Project, Message, etc) and have the file upload fields listed in a fields_for block.
My problem is that since I'm using jQuery-File-Uploader with AJAX the parent model's form will call the parent model's Create method when ever a file is uploaded. The rest of the parent model fields might not be filled out yet. I'm thinking maybe I could have the file uploader call the create method in the Assets controller, but then I would some how have to send the parent model's class so the polymorphic association is stored correctly.
Any ideas of how I might get this working cleanly? Thanks for looking.
OK.
add gem 'carrier wave'
to you Gemfile
save the code to /lib/flash_session_cookie_middleware.rb
require 'rack/utils'
class FlashSessionCookieMiddleware
def initialize(app, session_key = '_session_id')
@app = app
@session_key = session_key
end
def call(env)
if env['HTTP_USER_AGENT'] =~ /^(Adobe|Shockwave) Flash/
req = Rack::Request.new(env)
env['HTTP_COOKIE'] = [ @session_key,
req.params[@session_key] ].join('=').freeze unless req.params[@session_key].nil?
env['HTTP_ACCEPT'] = "#{req.params['_http_accept']}".freeze unless req.params['_http_accept'].nil?
end
@app.call(env)
end
end
edit session_store.rb
add the code to the end of file
Rails.application.config.middleware.insert_before(
ActionDispatch::Session::CookieStore,
FlashSessionCookieMiddleware,
Rails.application.config.session_options[:key]
)
Download jquery.uploadify.js from Uploadify and unzip it.
jquery.uploadify.v2.1.4.min.js
& swfobject.js
to
/app/assets/javascripts
if you use Rails3.1 or later, to
/public/javascripts
if you use Rails 3.0 or before version.uploadify.swf
and cancel.png
to /app/assets/images/
or
/public/images
uploadify.css
to /app/assets/stylesheets/
or
/public/stylesheets
Edit your application.js, insert below code to it
//= require swfobject
//= require jquery.uploadify
In you upload page, add this
<input id="uploadify" name="uploadify" type="file" />
add this code to you upload page
$(document).ready(function() {
<% key = Rails.application.config.session_options[:key] %>
var uploadify_script_data = {};
var csrf_param = $('meta[name=csrf-param]').attr('content');
var csrf_token = $('meta[name=csrf-token]').attr('content');
uploadify_script_data[csrf_param] = encodeURI(encodeURIComponent(csrf_token));
uploadify_script_data['<%= key %>'] = '<%= cookies[key] %>';
$('#uploadify').uploadify({
uploader : '/assets/uploadify.swf',
script : '/photos',
cancelImg : '/images/cancel.png',
auto : true,
multi : true,
removeCompleted : true,
scriptData : uploadify_script_data,
onComplete : function(event, ID, fileObj, doc, data) {
}
});
});
Write your controller like this
def create
@photo = Photo.new(:image => params[:Filedata])
@photo.save
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With