2017-04-10 37 views
0

我有一个使用ckeditor和cloudinary的rails 4项目。该uplaod到Cloudinary工作正常,但上传后,在编辑器应该小伙子的形象,我得到的错误:Rails4:CKEditor gsub错误

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 

当我去上传并点击“在服务器上查找图像”时,图像就在那里,我可以在编辑器上加载图像,但不能在将图像上传到服务器时使用

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

谢谢!

回答

0

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

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模型的破解。

将这个代码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 
+0

谢谢!没有lib/ckeditor文件夹。我应该创建一个? –

+0

我刚刚尝试创建文件:libs/ckeditor/asset_response.rb并将代码放在那里。但我仍然遇到同样的错误。我没有在我的项目中找到gem的CKEDITOR文件夹,所以我认为我应该在我的项目的LIB文件夹中创建这个文件? –

+0

是的,您必须在'lib' ckeditor/asset_response.rb文件中创建文件夹,并检查模型中是否存在ckeditor文件夹。 –