Your posts match “ pluploader-rails ” tag:

plupload-rails with carrierwave -- 實作筆記

一樣為了專案啊 ... (泣

為了練習英文,我試試看用英文寫筆記好了

Installing plupload-rails

  1. place gem 'plupload-rails' into your Gemfile and execute bundle install in your Terminal.app
  2. add following lines into your application.js and application.css
    • 'application.js'
    • //= require moxie
    • //= require plupload.dev
    • //= require plupload/i18n/de - for i18n ... maybe it has zh version :P
    • //= require plupload.settings - optional, but recommended ...
    • //= require jquery.plupload.queue - if you want to integrated with jQuery
    • 'application.css'
    • *= require jquery.plupload.queue
  3. Place following code on any view files you want.
<script>
  $(function(){
    $("#uploader").pluploadQueue({
      runtimes: 'html5,flash,silverlight',
      url: '<%= assets_path %>',
      multipart_params: {
        '<%= request_forgery_protection_token %>': '<%= form_authenticity_token %>',
        '<%= Rails.application.config.session_options[:key] %>': '<%= request.session_options[:id] %>'
      }
    });
  });
</script>

Okay ... that's it!

Controller / Model

I want to use the Carrierwave as my uploader, so first of all, I need to install it and make an uploader and I call it ImageUploader with Asset model.

So, I need to combine Asset model and ImageUploader together with path column, so in my asset.rb ...

class Asset < ActiveRecord::Base
  mount_uploader :path, ImageUploader
end

That's it!

And now, it's Controller's turn.

I have a controller called AssetsController and it has an action called create, that's do some magic in that action:

class AssetsController < ApplicationController

  def create
    asset = Asset.new(path: params[:file])
    result = asset.save
    
    render json: { result: result }
  end
  
end

okay ... that's it. If you want to have an association with User or Event ... etc, just feel free to add them, and add xxx_id in the JavaScript multipart_params settings, for example:

I want those images that I uploaded associated with current_user, I can make multipart_params like that:

multipart_params: {
        '<%= request_forgery_protection_token %>': '<%= form_authenticity_token %>',
        '<%= Rails.application.config.session_options[:key] %>': '<%= request.session_options[:id] %>',
        'user_id': <%= current_user.id =>
      }

See? So easy! Or you can do some magic with your url field, like url: '<%= event_images_path(@event) %>'

Okay ... That's it!

Learn more on https://github.com/gucki/plupload-rails