简体   繁体   中英

Rails4: CKEditor gsub error

I have a project in rails 4 that uses ckeditor with cloudinary. The uplaod to Cloudinary works fine, but after the upload, when the editor should lad the image, I get the error:

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

在此处输入图片说明

My CKEditor Picture Uploader is:

# 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. You have to create ckeditor asset_response.rb file in your lib/ckeditor folder of your App and put this line of code

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. This is just a hack to include Ckeditor::Picture model.

Put this code for cloudinary in CkeditorPictureUploader file

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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