首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Rails4: CKEditor gsub错误

Rails4: CKEditor gsub错误
EN

Stack Overflow用户
提问于 2017-04-11 01:33:57
回答 2查看 274关注 0票数 0

我在Rails4中有一个项目,它使用了ckeditor和cloudinary。上传到Cloudinary的uplaod运行良好,但是在上传之后,当编辑器应该加载图像时,我得到了错误:

代码语言:javascript
复制
NoMethodError - undefined method `gsub' for nil:NilClass:

我的CKEditor图片上传工具是:

代码语言:javascript
复制
# 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“时,图片就在那里,我可以在编辑器上加载图片,但不能在上传图片到服务器时加载。

以前有人遇到过这个问题吗?

谢谢!

EN

回答 2

Stack Overflow用户

发布于 2017-04-12 20:54:57

出现此错误的原因是gsub在ckeditor中找不到上传图像的url。您必须在您的应用程序的lib/ckeditor文件夹中创建ckeditor asset_response.rb文件,并将这行代码

代码语言:javascript
复制
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文件中

代码语言:javascript
复制
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
EN

Stack Overflow用户

发布于 2018-06-09 03:03:14

从@t-s的回答中,我发现在Ckeditor::AssetResponse#asset_url方法中,asset对象不会被重新加载,因此asset.content_url将始终为空,从而导致错误。我这样修复它:

代码语言:javascript
复制
class Ckeditor::Picture < Ckeditor::Asset
  ...
  def url_content
    url(:content) || begin
      if persisted?
        reload
        url(:content)
      end
    end
  end
end

如果你有Ckeditor::AttachmentFile类,它也是一样的。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43329496

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档