使用 Rails液滴中的 stylesheet_link_tag 和 javascript_include_tag
我希望能够让用户完全控制和编辑布局。我还希望他们能够包含他们想要的 javascript 插件。因此,我必须制作一个接口来允许他们这样做。
例如,默认的 html 看起来像一个更复杂的版本:
<head>
<title>{{site.name}}</title>
...
{{js_plugins.colorbox}} # this should return the necessary javascript and/or stylesheet tags
</head>
My Liquid JsPlugins drop 是这样的:
class JsPluginsDrop < Liquid::Drop
include ActionView::Helpers::AssetTagHelper
...
def colorbox
javascript_include_tag "/path/to/js"
end
end
当我运行我的规范时,我收到此错误(请注意,您会看到 @drop["colorbox-1.3. 15"]
当我上面提供的代码表现不同时。但是,我想简化我的代码,因为这不是问题,问题是 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 =>;他们给出的答案是真的
,如果可能的话,我如何在 drop 中使用 stylesheet_link_tag 和 javascript_include_tag ?
I want to be able to give the users full control and edit the layout. I also want them to be able to include what javascript plugins they want. Therefore, I had to make an interface to allow them to do that.
For example, the default html looks like a more complicated version of this:
<head>
<title>{{site.name}}</title>
...
{{js_plugins.colorbox}} # this should return the necessary javascript and/or stylesheet tags
</head>
My Liquid JsPlugins drop is like this:
class JsPluginsDrop < Liquid::Drop
include ActionView::Helpers::AssetTagHelper
...
def colorbox
javascript_include_tag "/path/to/js"
end
end
When I run my specs though, I get this error (note that you see @drop["colorbox-1.3.15"]
when the code I supplied above acts differently. However, I wanted to simplify my code since that's not the problem, it's the usage of the TagHelper
that is the problem):
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
I won't be surprised if the problem is caused by the fact that this is separate from my Rails environment, and the drop does not have access to the config
of Rails. Since I still want to be able to use these convenience methods and :cache => true
that they give, how can I use the stylesheet_link_tag and javascript_include_tag from within a drop, if it's possible at all?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来现在这样做是可能的:
It seems that this is possible now when done this way: