我正在尝试从我的 Sinatra Web 应用程序中将图像上传到 Cloudinary。我通过使用 CarrierWave 将图像保存在本地,我知道 Cloudinary 和 CarrierWave 可以很好地相互配合,但我已经尝试了所有方法,但没有骰子。
我已经关注了文档,但它们非常以 Rails 为中心。该死的Rails!
# Model - User.rb
class ImageUploader < CarrierWave::Uploader::Base
include Cloudinary::CarrierWave
process convert: 'png'
version :face do
cloudinary_transformation gravity: :face
end
end
class User
include DataMapper::Resource
attr_accessor :image
property.............
mount_uploader :image, ImageUploader
end
# Config - app.rb
require 'carrierwave/datamapper'
require 'cloudinary'
# DataMapper code ...
# Some routes ...
post '/add' do
user = user.create(name: params[:name])
Cloudinary::Uploader.upload(params[:image], api_key: 'API_KEY', api_secret: 'API_SECRET', cloud_name: 'CLOUD_NAME')
# View (haml)
%head
%script{src: "//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"}
%script{src: "js/cloudinary_js/jquery.ui.widget.js"}
%script{src: "js/cloudinary_js/jquery.iframe-transport.js"}
%script{src: "js/cloudinary_js/jquery.fileupload.js"}
%script{src: "js/cloudinary_js/jquery.cloudinary.js"}
# snip snip ...
%form{action: "/add", method: "POST", enctype: "multipart/form-data"}
%input{type: "file", name: "image"}
%button Submit
我在尝试提交表单时遇到的错误是:
TypeError at /add
no implicit conversion of Hash into String
它给我的导致错误的行是 post 方法中的 Cloudinary uploader 行。
这是我的 params[:image] 哈希
{:filename=>"batman_robin.jpg", :type=>"image/jpeg", :name=>"image", :tempfile=>#<Tempfile:/var/folders/rv/2w8gpstn4hb2lb__27n27ksh0000gn/T/RackMultipart20131223-36484-tgmaba>, :head=>"Content-Disposition: form-data; name=\"image\"; filename=\"batman_robin.jpg\"\r\nContent-Type: image/jpeg\r\n"}
在文档中,它提到了一个文件签名,然后声明 CarrierWave 处理这个。我认为我的问题在于上传表单。
我哪里错了?任何人都知道'cl_image_upload_tag'生成什么以及我需要添加到我的文件上传标签中的Cloudinary“特殊属性”是什么?
更新:所以我想到了,实际上我可以像这样直接传递图像:
Cloudinary::Uploader.upload(params[:image][:filename] ...
然而它现在说
No such file or directory - batman_robin.jpg
这很奇怪。我在那里硬编码了一个 URL,它被上传到 Cloudinary。有任何想法吗?另外,关于 Cloudinary jQuery 上传有什么想法吗?