2011-03-12 60 views
5

我希望能够给用户完全控制和编辑布局。我也希望他们能够包括他们想要的JavaScript插件。因此,我不得不做出一个界面来允许他们这样做。在液滴中使用Rails的stylesheet_link_tag和javascript_include_tag

例如,默认的HTML看起来像一个更复杂的版本是:

<head> 
    <title>{{site.name}}</title> 
    ... 
    {{js_plugins.colorbox}} # this should return the necessary javascript and/or stylesheet tags 
</head> 

我液体JsPlugins下降是这样的:

class JsPluginsDrop < Liquid::Drop 
    include ActionView::Helpers::AssetTagHelper 
    ... 
    def colorbox 
    javascript_include_tag "/path/to/js" 
    end 
end 

当我跑我的规格虽然,我得到这个错误(注意,当我上面提供的代码的行为不同时,你会看到​​)但是,我想简化我的代码,因为这不是问题,这是TagHelper的使用问题):

Failures: 
    1) JsPluginsDrop colorbox-1.3.15 should return the correct script tags 
    Failure/Error: @drop["colorbox-1.3.15"].stylesheets.should include("/jquery-plugins/colorbox-1.3.15/example1/colorbox.css") 
    undefined local variable or method `config' for #<JsPluginsDrop:0xcbfab38> 
    # ./app/drops/js_plugins_drop.rb:22:in `stylesheets' 
    # ./spec/models/js_plugins_drop_spec.rb:11 

如果问题是由于这是与我的Rails环境分开的事实而导致的,并且该drop无法访问Rails的config,我不会感到惊讶。由于我仍然希望能够使用这些便利方法,并且他们给出:cache => true如果可能,我如何才能从drop内使用stylesheet_link_tag和javascript_include_tag?

回答

2

看来,这是现在能够做的时候是这样的:

class MyDrop < Liquid::Drop 

    ... 
    def my_js_tag 
    helpers.javascript_include_tag '/some/thing' 
    end 
    ... 

    def helpers 
    @helpers ||= ActionController::Base.helpers 
    end 

end