使用CarrierWave和Rails设置Froala WYSIWYG编辑器
我一直试图让Froala完全使用我的rails设置。 我有一种类似于应用程序的博客,其中包含与每篇post相关的post和图片。
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,但我不知道它应该是什么。
$(function() { $('.selector').froalaEditor({ // Set the image upload URL. imageUploadURL: '????', imageUploadParams: { id: 'my_editor' } }) });
我整天都在研究这个问题并尝试了我能想到的一切。 任何帮助将非常感谢。 谢谢。
我使用了carrierwave和fog来上传到Amazon S3。 这是它的样子,我会跳过雾部分,你可能需要做一些调整。 但是,这个概念很简单。
我使用angularJS,但jquery选项应该是这样的。 您需要使用POST方法定义上传路径
javascript:
然后,您需要实现/attachment/upload.json。
在Rails中
-- attachment.rb class Attachment < ActiveRecord::Base mount_uploader :picture, PictureUploader end
因为它是ajax调用,所以在提交时需要在控制器中处理CSRF令牌validation。 此示例将跳过validation:因此在控制器中添加skip_before_filter:verify_authenticity_token。 如果您不想跳过validation。 您将需要使用imageUploadParams传递Froala初始化中的参数:{'authenticity_token':您的csrf标记}。 让我们来看看rails部分。
-- 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
使用rails生成PictureUploader并在控制台中创建模型
rails generate uploader Picture rails g model attachment picture:string rake db:migrate
在您的route.rb中,设置路径到您的控制器#方法
post 'attachment/upload' => 'attachments#upload'
因此,您将通过POST进行路由/附件/上传,并调用附件#upload。 希望能帮助到你! 如果有什么事让你困惑,请告诉我。
哦,假设你和我在froala和rails中陷入了同样的陷阱4.我建议你应该正确阅读CarrierWave文件
GitHub Carrierwave Respo
嗯,如果你正在寻找一个更详细的例子,我想出了这个。 这是非常好的IMO
允许使用CarrierWave进行文件上载
快乐的编码