Setting up Froala WYSIWYG editor with CarrierWave and Rails

落爺英雄遲暮 提交于 2019-11-30 23:42:54

I used the carrierwave and fog to upload to Amazon S3. Here is how it looks like,I'll skip the fog part and you may need to do some adjustments. However, the concept is easy.

I used angularJS, but the jquery options should look like this. You will need to define your upload route with POST method

The javascript:

<script>
    $(function() {
        $('.selector').froalaEditor({
            // Set the image upload URL.
            imageUploadURL: '/attachment/upload.json',
            imageUploadMethod: 'POST'
        })
    }
</script>

Then you will need to implement the /attachment/upload.json.

In Rails

-- attachment.rb
class Attachment < ActiveRecord::Base
    mount_uploader :picture, PictureUploader
end

Because it is ajax calling, you will need to handle CSRF token verify in your controller when you submit. This example will skip the verfication: so add skip_before_filter :verify_authenticity_token in your controller. If you don't want to skip the verification. you will need to pass the parameter in the Froala initialization with imageUploadParams: {'authenticity_token': your csrf token}. So let's go over the rails part.

-- 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

Use rails generate a PictureUploader and create model in console

rails generate uploader Picture
rails g model attachment picture:string
rake db:migrate

In your route.rb, setup the route to your controller#method

post 'attachment/upload' => 'attachments#upload'

So you will have a route /attachment/upload through POST, and it call the attachment#upload. Hope it helps! Let me know if anything confuse you.

Oh well assuming that you're in the same pitfall with me in froala & rails 4. I'm suggesting that you should read the CarrierWave document properly

GitHub Carrierwave Respo

Uhm if you're looking for a more detail example I come up with this one. It's pretty good IMO

Allowing File Uploads with CarrierWave

Happy Coding

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!