简体   繁体   English

Rails4:CKEditor gsub错误

[英]Rails4: CKEditor gsub error

I have a project in rails 4 that uses ckeditor with cloudinary. 我在Rails 4中有一个使用ckeditor和cloudinary的项目。 The uplaod to Cloudinary works fine, but after the upload, when the editor should lad the image, I get the error: 升级到Cloudinary可以正常工作,但是在上传之后,当编辑器应该对图像进行粘贴时,出现错误:

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

在此处输入图片说明

My CKEditor Picture Uploader is: 我的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

When I go to upload and click on "find image on server" the image is there and I can load the image on editor, but not when I upload image to server 当我上载并单击“在服务器上查找图像”时,该图像已存在,我可以在编辑器中加载该图像,但是当我将图像上载到服务器时不

Someone got this problem before? 以前有人遇到过这个问题吗?

Thanks! 谢谢!

This error occurs because gsub could not find url for Uploaded image in ckeditor. 发生此错误是因为gsub在ckeditor中找不到上载图像的URL。 You have to create ckeditor asset_response.rb file in your lib/ckeditor folder of your App and put this line of code 您必须在应用程序的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

put the whole code and replace asset_url method with this. 放入整个代码,并用此代码替换asset_url方法。 This is just a hack to include Ckeditor::Picture model. 这只是一个包含Ckeditor::Picture模型的技巧。

Put this code for cloudinary in CkeditorPictureUploader file 将这段代码用于cloudinary放入CkeditorPictureUploader文件中

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

From @ts 's answer, I found that in Ckeditor::AssetResponse#asset_url method, the asset object is not reloaded, so asset.content_url will always be nil thus caused the error. 从@ts的答案中,我发现在Ckeditor :: AssetResponse#asset_url方法中, asset对象未重新加载,因此asset.content_url将始终为nil,从而导致错误。 I fixed it like this: 我这样修复:

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

And similarly for Ckeditor::AttachmentFile class if you have it. 对于Ckeditor::AttachmentFile类,如果有的话,也类似。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM