首页 文章

Rails4:CKEditor gsub错误

提问于
浏览
0

我在rails 4中有一个使用带有cloudinary的ckeditor的项目 . Cloudinary的上传工作正常,但上传后,当编辑器应该播放图像时,我收到错误:

NoMethodError - undefined method `gsub' for nil:NilClass:

enter image description here

我的CKEditor图片上传器是:

# encoding: utf-8
class CkeditorPictureUploader < CarrierWave::Uploader::Base
  include Ckeditor::Backend::CarrierWave
  include Cloudinary::CarrierWave
  include CarrierWave::MiniMagick



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

当我上传并点击“在服务器上查找图像”时,图像就在那里我可以在编辑器上加载图像,但是当我将图像上传到服务器时却没有

有人之前遇到过这个问题吗?

谢谢!

2 回答

  • 0

    发生此错误是因为 gsub 无法在ckeditor中找到上传图像的网址 . 你必须在你的应用程序的lib / ckeditor文件夹中创建ckeditor asset_response.rb文件并放入这行代码

    def asset_url(relative_url_root)
      @ckeditor_assets = Ckeditor::Picture.last.url_content
      puts @ckeditor_assets.inspect
      #return nil if asset.url_content.nil?
      url =  @ckeditor_assets #Ckeditor::Utils.escape_single_quotes(asset.url_content)
    
      if URI(url).relative?
        "#{relative_url_root}#{url}"
      else
        url
      end
    end
    

    放入整个代码并用此替换asset_url方法 . 这只是一个包含 Ckeditor::Picture 模型的黑客 .

    在CkeditorPictureUploader文件中将此代码用于cloudinary

    include Ckeditor::Backend::CarrierWave
    include Cloudinary::CarrierWave 
       process :tags => ["photo_album_sample"]
       process :convert => "jpg"
       version :thumbnail do
         eager
         resize_to_fit(200, 200)
         cloudinary_transformation :quality => 80          
       end
    
  • 0

    从@ t-s的回答中,我发现在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 类,如果你有它,同样如此 .

相关问题