@huobazi 我在 Google -> github.com -> 找到你的 demo。 我 clone 下来在本地能跑起来,但是我自己的相册里面报错:
Started POST "/admin/pictures?album_id=5177e89f204468b3ca00000c&authenticity_token=J9VQ54DtwiumdjIDs8JVFy9OqE%2FKTaXf%2Fl8rgA6cya4%3D&qqfile=4774740_1294676570a.jpg" for 127.0.0.1 at 2013-04-26 02:14:24 +0800
Processing by Admin::PicturesController#create as */*
Parameters: {"album_id"=>"5177e89f204468b3ca00000c", "authenticity_token"=>"J9VQ54DtwiumdjIDs8JVFy9OqE/KTaXf/l8rgA6cya4=", "qqfile"=>"4774740_1294676570a.jpg"}
MOPED: 127.0.0.1:27017 COMMAND database=admin command={:ismaster=>1} (0.7401ms)
MOPED: 127.0.0.1:27017 QUERY database=summer_development collection=users selector={"$query"=>{"_id"=>"516d636a2044683ecc000001"}, "$orderby"=>{:_id=>1}} flags=[:slave_ok] limit=-1 skip=0 batch_size=nil fields=nil (0.3171ms)
Completed 200 OK in 14ms (Views: 0.1ms)
以下是我的 Gemfile:
gem 'carrierwave'
gem 'carrierwave-mongoid', :require => 'carrierwave/mongoid'
gem 'mini_magick'
gem 'rack-raw-upload'
app/uploaders/image_uploader.rb
# encoding: utf-8
require "digest/md5"
require "carrierwave/processing/mini_magick"
class PictureUploader < CarrierWave::Uploader::Base
include CarrierWave::MiniMagick
storage :file
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
process :resize_to_fit => [1100, 700]
version :thumb do
process :resize_to_fill => [250, 250]
end
version :thumb_small, :from => :thumb do
process :resize_to_fill => [150, 150]
end
def extension_white_list
%w(jpg jpeg gif png)
end
def filename
if super.present?
@name ||= Digest::MD5.hexdigest(File.dirname(current_path))
"#{@name}.#{file.extension.downcase}"
end
end
end
app/models/picture.rb
class Picture
include Mongoid::Document
include Mongoid::Timestamps::Created
include Rails.application.routes.url_helpers
field :image, type: String
field :album_id, type: Integer
attr_accessible :image, :image_cache, :album_id
validates :image, :presence => true
belongs_to :album
mount_uploader :image, PictureUploader
end
app/controllers/admin/pictures_controller.rb
def create
file = params[:qqfile].is_a?(ActionDispatch::Http::UploadedFile) ? params[:qqfile] : params[:file]
@picture = Picture.new(:album_id => params[:album_id])
@picture.image = file
@album = @pictrue.album
if @picture.save
render json: { success: true, src: @picture.to_json }
else
render json: @picture.errors.to_json
end
end
app/views/admin/pictures/create.js.erb
$(".pictures").html("<%=j render 'admin/pictures/picture' %>");
app/views/admin/pictures/_form.html.slim
p style="margin: 0"
= link_to "取消(返回相册)", admin_album_path(@picture.album_id), :remote => true
small 只能上传[jpg, jpeg, gif, png]文件,并且单个图片最大1M.
#file-uploader
javascript:
$(document).ready(function(){
var uploader = new qq.FileUploader({
debug: true,
allowedExtensions: ['jpg', 'jpeg', 'png', 'gif'],
sizeLimit: 1048576,
minSizeLimit: 0,
multiple: true,
element: document.getElementById('file-uploader'),
action: '#{admin_pictures_path}',
onComplete: function(id, fileName, responseJSON){
$.getScript("#{admin_pictures_path}");
},
onSubmit: function(id, fileName) {
uploader.setParams({
album_id: "#{@picture.album_id}",
authenticity_token: "#{form_authenticity_token.to_s}"
});
}
});
});
这是你的 demo 里面正常上传
Started POST "/screenshots?xx=xx&yy=yy&zz=zz&authenticity_token=smM3tYpYq9QgUa0JVJXqK%2BWUEPL2VOQ8Cnu0bKK0Kww%3D&qqfile=4774740_1294676570a.jpg" for 127.0.0.1 at 2013-04-26 02:07:09 +0800
Processing by ScreenshotsController#create as */*
Parameters: {"file"=>#<ActionDispatch::Http::UploadedFile:0x007fa4564369b0 @original_filename="4774740_1294676570a.jpg", @content_type="application/octet-stream", @headers=nil, @tempfile=#<File:/var/folders/rd/_91ws2bd3y5195l685m841f40000gn/T/raw-upload.20130426-27154-11caq4q>>, "xx"=>"xx", "yy"=>"yy", "zz"=>"zz", "authenticity_token"=>"smM3tYpYq9QgUa0JVJXqK+WUEPL2VOQ8Cnu0bKK0Kww=", "qqfile"=>"4774740_1294676570a.jpg"}