Your posts match “ CarrierWave ” tag:

plupload + carrierwave 實作筆記 (二)

真的就只是個小筆記而已 ... 這邊的 code 我寫得很髒,所以就不直接貼了,大概講一下做法。

第一步: 先幹出一個 AssetsController 負責接圖片

def create
  ea = EventImage.new
  ea.path = params[:file]
  ea.save

  render json: ea
end

第二步:

plupload 那邊基本上要自己幹一個上傳器出來,然後監聽 FileUploaded 事件,這個事件會傳三個參數回來,分別是: up, file, info 三個,第一個 up 應該就是 uploader 本身,第二個 file 是你上傳上去的那個 file ,而 info 包含了回傳的內容,其中 response 是純字串,如果你的 Server 會吐 JSON 回來,記得這邊上一下 JSON.parse 後才能用

補充記錄:

  1. 如果要做什麼檢查的話,譬如數量,可以用 up.files 去取得設定過上傳的檔案的陣列
  2. 可以用 up.removeFile(up.files[index]) 去刪除某個檔案
  3. 如果要選擇檔案後就可以上傳,那可以把 up.start(); 加在 FilesAdded 事件上
  4. 在透過 new plupload.Uploader() 建立上傳器的時候可以透過指定 browse_button 來選擇要觸發上傳功能的按鈕,然後就是其實我不知道 container 到底幹啥用的 ....

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