CKeditor上传到S3,但无法在编辑器中访问 - RAILS - Carrierwave

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (60)

rails 5.1.4 ruby​​ 2.4.1 ckeditor 4.2.4 carrierwave 1.2.2 minimagick 4.8.0 fog-aws 2.0.1

我有carwave支持的ckeditor,它连接到S3存储桶。图像正在上载到S3存储桶。我可以看到那里的文件!但是ckeditor屏幕冻结了,这是heroku logs..rails服务器的输出给出了同样的错误。这绝对是一个ckeditor配置问题,因为我可以通过照片上传器上传封面照片,其中ckeditor不是中间人。它只是一个使用carrierwave和S3的html文件字段。我能够回忆起封面照片,并从S3桶中显示出来,所以我知道我的凭据是好的。

HEROKU日志:

Started POST "/ckeditor/pictures?&CKEditor=ckeditor&CKEditorFuncNum=1&langCode=en" for IP ADDRESS

Processing by Ckeditor::PicturesController#create as HTML

Parameters: {"upload"=>#<ActionDispatch::Http::UploadedFile:0x000000038f2868 @tempfile=#<Tempfile:/tmp/RackMultipart20180315-4-1xodi16.jpg>, @original_filename="coloursquare.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"upload\"; filename=\"coloursquare.jpg\"\r\nContent-Type: image/jpeg\r\n">, "ckCsrfToken"=>"56COpAWYrezg8OINmQYCmH6Hr1I5ZVxy1u9YtFmP", "CKEditor"=>"ckeditor", "CKEditorFuncNum"=>"1", "langCode"=>"en"}

DEBUG -- 

DEBUG -- SQL INSERT INTO "ckeditor_assets" ("data_file_name", "data_content_type", "data_file_size", "type", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6) RETURNING "id"[0m  [["data_file_name", "coloursquare.jpg"], ["data_content_type", "image/jpeg"], ["data_file_size", 12893], ["type", "Ckeditor::Picture"], ["created_at", "2018-03-15 23:25:06.970758"], ["updated_at", "2018-03-15 23:25:06.970758"]]

heroku[router]: at=info method=POST path="/ckeditor/pictures?&CKEditor=ckeditor&CKEditorFuncNum=1&langCode=en" host=stormy-hollows-12083.herokuapp.com request_id=0e9fd60d-685e-4cd5-9296-413b68e0bf45 fwd="73.96.105.72" dyno=web.1 connect=0ms service=274ms status=500 bytes=1825 protocol=https

DEBUG -- COMMIT

INFO -- Completed 401 Unauthorized in 261ms (ActiveRecord: 27.2ms)

FATAL -- 

FATAL -- : ArgumentError (Version content doesn't exist!):

FATAL --  

FATAL -- : app/models/ckeditor/asset.rb:4:in `url'

app/models/ckeditor/picture.rb:5:in `url_content'

CKEDITOR PICTURE UPLOADER.rb

class CkeditorPictureUploader < CarrierWave::Uploader::Base
  include Ckeditor::Backend::CarrierWave  
  include CarrierWave::MiniMagick

  storage :fog

  def store_dir
    "uploads/ckeditor/pictures/#{model.id}"
  end

  def default_url
    "/images/fallback/" + [version_name, "default.png"].compact.join('_')
  end

  def content_type_blacklist
    ['application/text', 'application/json']
  end

  def extension_white_list
    Ckeditor.image_file_types
  end

  def content_type_blacklist
    ['application/text', 'application/json']
  end
end

CKEDITOR config.js

CKEDITOR.editorConfig = function( config ) {
  config.language = 'en';
  config.uiColor = '#ffffff';
  config.pasteFromWordRemoveStyles = false;
  config.filebrowserBrowseUrl = "/ckeditor/attachment_files";
  config.filebrowserFlashBrowseUrl = "/ckeditor/attachment_files";
  config.filebrowserFlashUploadUrl = "/ckeditor/attachment_files";
  config.filebrowserImageBrowseLinkUrl = "/ckeditor/pictures";
  config.filebrowserImageBrowseUrl = "/ckeditor/pictures";
  config.filebrowserImageUploadUrl = "/ckeditor/pictures?";
  config.filebrowserUploadUrl = "/ckeditor/attachment_files";
  config.allowedContent = true;
}

MODEL CKEDITOR asset.rb

class Ckeditor::Asset < ActiveRecord::Base
  include Ckeditor::Orm::ActiveRecord::AssetBase

  delegate :url, :current_path, :content_type, to: :data

  validates :data, presence: true
end

MODEL CKEDITOR picture.rb

class Ckeditor::Picture < Ckeditor::Asset
  mount_uploader :data, CkeditorPictureUploader, mount_on: :data_file_name

  def url_content
    url(:content)
  end
end
提问于
用户回答回答于

我发现在Ckeditor :: AssetResponse#asset_url 方法中,asset对象没有重新加载,因此asset.content_url总是为nil因此导致错误。我这样修好了:

class Ckeditor::Picture < Ckeditor::Asset
  ...
  def url_content
    url(:content) || begin
      if persisted?
        reload
        url(:content)
      end
    end
  end
end

扫码关注云+社区

领取腾讯云代金券