首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >CKEditor载波云图

CKEditor载波云图
EN

Stack Overflow用户
提问于 2013-06-21 06:51:25
回答 4查看 2.2K关注 0票数 4

我正在尝试让CKEditor与Carrierwave和Cloudinary一起工作。到目前为止,带有常规文件上传域的未启用CKEditor的视图可以与Carrierwave和Cloudinary完美地协同工作。但是,当我尝试在CKEditor中上传一个文件并“将其发送到服务器”时,我得到了一个NoMethodError - undefined method 'each' for "image/jpeg":String:

在从CKEditor类中删除本地存储配置之前,它正在工作,但将文件保存在本地。

这里是我当前的CKEditor上传程序:

代码语言:javascript
复制
class CkeditorAttachmentFileUploader < CarrierWave::Uploader::Base
  include Cloudinary::CarrierWave
  include Ckeditor::Backend::CarrierWave

  def extension_white_list
    Ckeditor.attachment_file_types
  end
end

日志文件:

代码语言:javascript
复制
Started POST "/ckeditor/pictures?CKEditor=subsection_content&CKEditorFuncNum=3&langCode=en&authenticity_token=5Bt06UwjUD%2FEdLFANBmZojdv8Hvn2GbQRLvC6h11Dd8%3D" for 127.0.0.1 at 2013-06-20 15:44:18 -0700
Processing by Ckeditor::PicturesController#create as HTML
  Parameters: {"upload"=>#<ActionDispatch::Http::UploadedFile:0x007ff742c77018 @original_filename="pic1.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"upload\"; filename=\"pic1.jpg\"\r\nContent-Type: image/jpeg\r\n", @tempfile=#<Tempfile:/var/folders/0t/l1qc3j596v77_z3s8f2pm75w0000gn/T/RackMultipart20130620-18566-i0av53>>, "CKEditor"=>"subsection_content", "CKEditorFuncNum"=>"3", "langCode"=>"en", "authenticity_token"=>"5Bt06UwjUD/EdLFANBmZojdv8Hvn2GbQRLvC6h11Dd8="}
   (0.4ms)  BEGIN
   (0.4ms)  ROLLBACK
Completed 500 Internal Server Error in 4ms

NoMethodError - undefined method `each' for "image/jpeg":String:
  (gem) cloudinary-1.0.59/lib/cloudinary/carrier_wave/process.rb:100:in `block in transformation'

任何想法都很感谢!

更新-来自Tal Lev-Ami answer的工作配置

这是我当前正在工作的ckeditor_picture_uploader.rb文件:

代码语言:javascript
复制
# encoding: utf-8
class CkeditorPictureUploader < CarrierWave::Uploader::Base
  include Ckeditor::Backend::CarrierWave
  include Cloudinary::CarrierWave

  # Include RMagick or ImageScience support:
  # include CarrierWave::RMagick
  include CarrierWave::MiniMagick
  # include CarrierWave::ImageScience

  # Choose what kind of storage to use for this uploader:
  # storage :file

  # Override the directory where uploaded files will be stored.
  # This is a sensible default for uploaders that are meant to be mounted:
  # def store_dir
  #   "uploads/ckeditor/pictures/#{model.id}"
  # end

  # Provide a default URL as a default if there hasn't been a file uploaded:
  # def default_url
  #   "/images/fallback/" + [version_name, "default.png"].compact.join('_')
  # end

  # Process files as they are uploaded:
  # process :scale => [200, 300]
  #
  # def scale(width, height)
  #   # do something
  # end

  [:extract_content_type, :set_size, :read_dimensions].each do |method|
    define_method :"#{method}_with_cloudinary" do
      send(:"#{method}_without_cloudinary") if self.file.is_a?(CarrierWave::SanitizedFile)
      {}
    end
    alias_method_chain method, :cloudinary
  end

  process :read_dimensions

  # Create different versions of your uploaded files:
  version :thumb do
    process :resize_to_fill => [118, 100]
  end

  version :content do
    process :resize_to_limit => [800, 800]
  end

  # Add a white list of extensions which are allowed to be uploaded.
  # For images you might use something like this:
  def extension_white_list
    Ckeditor.image_file_types
  end
end
EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2013-06-21 14:16:04

尝试将以下代码添加到CkPictureUploader/CkeditorAttachmentFileUploader:中

代码语言:javascript
复制
[:extract_content_type, :extract_size, :extract_dimensions].each do |method|
  define_method :"#{method}_with_cloudinary" do
    send(:"#{method}_without_cloudinary") if self.file.is_a?(CarrierWave::SanitizedFile)
    {}
  end
  alias_method_chain method, :cloudinary
end
票数 5
EN

Stack Overflow用户

发布于 2016-09-13 22:53:34

Ckeditor已更新,因此根据ckeditor gem版本的不同,可能需要extract_size和extract_dimensions

在修改的地方提交:https://github.com/galetahub/ckeditor/blob/4e6d8413cc71f40d2d58ab3d0cb8dad19dd96894/lib/ckeditor/backend/carrierwave.rb

即:

代码语言:javascript
复制
[:extract_content_type, :extract_size, :extract_dimensions].each do |method|
  define_method :"#{method}_with_cloudinary" do
  send(:"#{method}_without_cloudinary") if self.file.is_a?  (CarrierWave::SanitizedFile)
    {}
 end
  alias_method_chain method, :cloudinary
end
票数 4
EN

Stack Overflow用户

发布于 2018-06-09 03:32:41

我对这些宝石的组合也有问题。编辑您的CkeditorAttachmentFileUploader,使其如下所示:

代码语言:javascript
复制
class CkeditorAttachmentFileUploader < CarrierWave::Uploader::Base
  include Ckeditor::Backend::CarrierWave
  include Cloudinary::CarrierWave

  [:extract_content_type, :extract_size, :extract_dimensions].each do |method|
    define_method :"#{method}_with_cloudinary" do
      send(:"#{method}_without_cloudinary") if self.file.is_a?(CarrierWave::SanitizedFile)
      {}
    end
    alias_method :"#{method}_without_cloudinary", method
    alias_method method, :"#{method}_with_cloudinary"
  end

  def extension_white_list
    Ckeditor.attachment_file_types
  end
end

在那之后,你会发现另一个错误。我发现在Ckeditor::AssetResponse#asset_url方法中,asset对象不会重新加载,因此asset.content_url将始终为空,从而导致错误。我是这样修复的:

代码语言:javascript
复制
class Ckeditor::Picture < Ckeditor::Asset
  ...
  def url_content
    url(:content) || begin
      if persisted?
        reload
        url(:content)
      end
    end
  end
end

如果你有Ckeditor::AttachmentFile类,它也是一样的。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17225138

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档