无法在Lib文件中访问Cloudinary Helper

时间:2014-03-21 07:56:29

标签: ruby-on-rails cloudinary

我正在尝试扩展RedCarpet markdown解析器,以使用我在应用程序中创建的媒体库中的图像。

我使用Cloudinary作为图像存储/ CDN,并且有一个Media表,用于存储Cloudinary中图像的公共ID。这一切都很好。

我在/ lib下创建了一个名为reddress.rb的文件,其中包含以下代码。我已经使用它一段时间来呈现markdown格式的文本没有问题。但是,现在我正在尝试使用cloudinary方法使用通过其Id引用媒体记录的短代码来扩展markdown。

问题是代码无法在cloudinary助手中找到cl_image_tag方法,即使需要.rb文件也是如此。

require 'redcarpet'
require 'cloudinary'
require "cloudinary/helper"

class RedDress
    def initialize
    end

    def format_as_html(txt)
        markdown = Redcarpet::Markdown.new(ExtendedMarkdownHTML, :autolink => true, :space_after_headers => true)
        markdown.render(txt)
    end
end

class ExtendedMarkdownHTML < Redcarpet::Render::HTML
    def preprocess(full_document)

        full_document.gsub!(/\[media (\d+)\]/) { |m|
            media = Media.find($1)
            cl_image_tag(media.image_id, :crop => :fill, :width => 80, :height => 80) unless media.nil?
        }
        full_document
    end
end

正在调用lib,并且没有cl_image_tag就可以了,我已经重新启动了服务器(每次都要进行更改。)

我正在使用Rails 4和Ruby 2.0.0。

有什么建议吗?

1 个答案:

答案 0 :(得分:2)

好的,经过一番摆弄后,我提出了基于http://support.cloudinary.com/entries/25418221-How-do-I-use-the-Cloudinary-helpers-from-the-Ruby-GEM-using-Sinatra-

的解决方案。

在ExtendedMarkdownHTML类中,我将Cloudinary Helper包括在内。

...
class ExtendedMarkdownHTML < Redcarpet::Render::HTML
include CloudinaryHelper

    def preprocess(full_document)
    ...

我还将文件移出/ lib文件夹到/ app / services,这是其他地方的一些建议。

希望这有帮助。

相关问题