【问题标题】:Rails4: CKEditor gsub errorRails4:CKEditor gsub 错误
【发布时间】:2017-04-10 17:33:57
【问题描述】:

我在 rails 4 中有一个项目,它使用 ckeditor 和 cloudinary。上传到 Cloudinary 工作正常,但上传后,当编辑器应该加载图像时,我收到错误:

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

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

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

谢谢!

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-4 ckeditor gsub


    【解决方案1】:

    出现此错误是因为gsub 在 ckeditor 中找不到上传图片的 url。您必须在 App 的 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
    

    【讨论】:

    • 谢谢!没有 lib/ckeditor 文件夹。我应该创建一个?
    • 我只是尝试创建文件:libs/ckeditor/asset_response.rb 并将代码放在那里。但我仍然遇到同样的错误。我在我的项目中没有找到 gem 的 CKEDITOR 文件夹,所以我想我应该在我的项目的 LIB 文件夹中创建这个文件?
    • 是的,您必须在libckeditor/asset_response.rb 文件中创建文件夹并检查模型中是否存在ckeditor 文件夹。
    • 嗨,Thnaks!我这样做了。我创建文件/lib/ckeditor/asset_response.rb 并检查文件夹/model/ckeditor。但我仍然收到错误消息:NoMethodError at /pictures undefined method gsub for nil:NilClassCkeditorPictureUploader 的代码也在那里,但错误仍然存​​在。
    • 您能否尝试在asset_response.rb 的每个函数处破解代码并从控制台检查所有值是否已成功保存在数据库中。从initialize(asset, request)开始
    【解决方案2】:

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

    class Ckeditor::Picture < Ckeditor::Asset
      ...
      def url_content
        url(:content) || begin
          if persisted?
            reload
            url(:content)
          end
        end
      end
    end
    

    如果你有 Ckeditor::AttachmentFile 类,同样如此。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多