【问题标题】:CKEditor Carrierwave CloudinaryCKEditor Carrierwave Cloudinary
【发布时间】:2013-06-17 23:43:32
【问题描述】:

我正在尝试让 CKEditor 与 Carrierwave 和 Cloudinary 一起使用。到目前为止,具有常规文件上传字段的非 CKEditor 启用视图与 Carrierwave 和 Cloudinary 完美配合。但是,当我尝试在 CKEditor 中上传文件并“将其发送到服务器”时,我收到了 NoMethodError - undefined method 'each' for "image/jpeg":String:

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

这是我当前的 CKEditor 上传器:

class CkeditorAttachmentFileUploader < CarrierWave::Uploader::Base
  include Cloudinary::CarrierWave
  include Ckeditor::Backend::CarrierWave

  def extension_white_list
    Ckeditor.attachment_file_types
  end
end

日志文件:

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 答案的工作配置

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

# 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

【问题讨论】:

    标签: ruby-on-rails ckeditor carrierwave cloudinary


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      Ckeditor 已更新,因此这可能需要为 extract_size 和 extract_dimensions,具体取决于 ckeditor gem 版本

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

      即:

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

      【讨论】:

        【解决方案3】:

        我对这些宝石的组合也有疑问。编辑您的 CkeditorAttachmentFileUploader 使其看起来与此类似:

        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 将始终为 nil 从而导致错误。我是这样修复的:

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

        如果你有 Ckeditor::AttachmentFile 类,同样如此。

        【讨论】:

          【解决方案4】:

          在这里查看我的评论

          https://github.com/galetahub/ckeditor/issues/670#issuecomment-301218366

          # encoding: utf-8
          class CkeditorPictureUploader < CarrierWave::Uploader::Base
            include Ckeditor::Backend::CarrierWave
            include CarrierWave::MiniMagick
            include Cloudinary::CarrierWave
          
            process :extract_dimensions
          
            [: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
          
            version :thumb do
              process :resize_to_fill => [118, 100]
            end
          
            version :content do
              process :resize_to_limit => [800, 800]
            end
          
            def extension_white_list
              Ckeditor.image_file_types
            end
          end
          

          【讨论】:

            猜你喜欢
            • 2012-09-15
            • 2015-10-22
            • 2018-05-19
            • 2018-06-09
            • 2016-12-22
            • 1970-01-01
            • 2013-09-01
            • 2017-10-08
            • 2019-01-24
            相关资源
            最近更新 更多