我用carrierwave和雾上传到Amazon S3。这是它的样子,我会跳过雾的一部分,你可能需要做一些调整。但是,这个概念很简单。
我用angularJS,但jQuery选项应该看起来像这样。你需要用POST方法定义上传路线
的JavaScript:
<script>
$(function() {
$('.selector').froalaEditor({
// Set the image upload URL.
imageUploadURL: '/attachment/upload.json',
imageUploadMethod: 'POST'
})
}
</script>
然后,你将需要实现/attachment/upload.json。
在Rails
-- attachment.rb
class Attachment < ActiveRecord::Base
mount_uploader :picture, PictureUploader
end
因为它是AJAX调用,您将需要处理CSRF在控制器中的令牌验证当您提交。此示例将跳过验证:所以在您的控制器中添加 skip_before_filter:verify_authenticity_token。如果你不想跳过验证。您将需要在imageUploadParams中使用Froala初始化参数传递参数:{'authenticity_token':您的csrf标记}。所以我们来看看导轨部分。
-- attachments_controller.rb
class AttachmentsController < ApplicationController
skip_before_filter :verify_authenticity_token
...
def upload
# The Model: Attachment, created below.
@attachment = Attachment.new
@attachment.picture = params[:file]
@attachment.save
respond_to do |format|
format.json { render :json => { status: 'OK', link: @attachment.picture.url}}
end
end
...
end
使用导轨产生PictureUploader并创建控制台
rails generate uploader Picture
rails g model attachment picture:string
rake db:migrate
模型在route.rb,设置路由到控制器#方法
post 'attachment/upload' => 'attachments#upload'
所以,你将有一个通过POST路由/附件/上传,并且它调用附件#上传。希望能帮助到你!让我知道是否有什么让你感到困惑。
你知道吗?也停留在这个http:// stackoverflow。com/questions/37518201/rails-4-how-to-have-image-upload-in-froala-editor-with-carrierwave/ – Rob
你可以在下面的回答 –