2017-04-10 58 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文件夾。 –