In Rails, how do I handle multiple, polymorphic file downloads with jquery-file and CarrierWave downloads?

In my project, I currently have different models (Project, Message, etc.) that:

has_many :assets, :as => :attachable, :dependent => :destroy

Each object is basically a model with a CarrierWave file. I usually just used accept_nested_attributes in the parent model (Project, Message, etc.) and had the file upload fields listed in the fields_for block.

My problem is that since I am using jQuery-File-Uploader with AJAX, the form of the parent model will call the parent model. Create a method when a file is ever loaded. The remaining fields of the parent model are not yet filled. I think maybe I could have the file loader call the create method in the Assets controller, but then I would like to somehow send the parent model class so that the polymorphic association is saved correctly.

Any ideas on how I can get this job clean? Thanks for watching.

+5
source share
1 answer

OK

Step 1

add gem 'carrier wave'gemfile to you

Step 2

save the code until /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

Step3

session_store.rb

Rails.application.config.middleware.insert_before(
  ActionDispatch::Session::CookieStore,
  FlashSessionCookieMiddleware,
  Rails.application.config.session_options[:key]
)

Step4

jquery.uploadify.js Uploadify .

Step5

  • jquery.uploadify.v2.1.4.min.js swfobject.js /app/assets/javascripts, Rails3.1 , /public/javascripts, Rails 3.0 .
  • uploadify.swf cancel.png /app/assets/images/ /public/images
  • uploadify.css /app/assets/stylesheets/ /public/stylesheets

Step6

application.js,

//= require swfobject
//= require jquery.uploadify

Step7

<input id="uploadify" name="uploadify" type="file" />

Step8

$(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) {
    }
  });
});

Step9

def create
  @photo = Photo.new(:image => params[:Filedata])
  @photo.save
end
+4

All Articles