我在Rails4中有一个项目,它使用了ckeditor和cloudinary。上传到Cloudinary的uplaod运行良好,但是在上传之后,当编辑器应该加载图像时,我得到了错误:
NoMethodError - undefined method `gsub' for nil:NilClass:
我的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
当我去上传并点击"find image on server“时,图片就在那里,我可以在编辑器上加载图片,但不能在上传图片到服务器时加载。
以前有人遇到过这个问题吗?
谢谢!
发布于 2017-04-12 20:54:57
出现此错误的原因是gsub
在ckeditor中找不到上传图像的url。您必须在您的应用程序的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
模型的技巧。
将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
发布于 2018-06-09 03:03:14
从@t-s的回答中,我发现在Ckeditor::AssetResponse#asset_url方法中,asset
对象不会被重新加载,因此asset.content_url
将始终为空,从而导致错误。我这样修复它:
class Ckeditor::Picture < Ckeditor::Asset
...
def url_content
url(:content) || begin
if persisted?
reload
url(:content)
end
end
end
end
如果你有Ckeditor::AttachmentFile
类,它也是一样的。
https://stackoverflow.com/questions/43329496
复制相似问题