2015-10-22 44 views
4

我一直试图让Froala完全使用我的导轨设置。我有一种类似博客的应用程序,其中包含与每篇文章相关的帖子和​​图片。使用CarrierWave和Rails设置Froala WYSIWYG编辑器

class Post < ActiveRecord::Base 
has_many :images 
accepts_nested_attributes_for :images 

class Image < ActiveRecord::Base 
belongs_to :post 
mount_uploader :image, ImageUploader 

我想弄清楚如何让这与Froala合作。我可以在Froala配置中设置上传URL,但我不知道它应该是什么。

<script> 
    $(function() { 
    $('.selector').froalaEditor({ 
     // Set the image upload URL. 
     imageUploadURL: '????', 

     imageUploadParams: { 
     id: 'my_editor' 
     } 
    }) 
    }); 
</script> 

我一直在研究这一整天,并尝试了所有我能想到的。任何帮助将非常感激。谢谢。

+0

你知道吗?也停留在这个http:// stackoverflow。com/questions/37518201/rails-4-how-to-have-image-upload-in-froala-editor-with-carrierwave/ – Rob

+0

你可以在下面的回答 –

回答

5

我用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路由/附件/上传,并且它调用附件#上传。希望能帮助到你!让我知道是否有什么让你感到困惑。

+0

找到上传url和实现例子:要点](https://gist.github.com/qqnc/c4417aefe120374c8271) –

+0

谢谢你的男人!很有帮助! –