使用 Rails液滴中的 stylesheet_link_tag 和 javascript_include_tag

发布于 2024-10-21 06:54:23 字数 1328 浏览 7 评论 0原文

我希望能够让用户完全控制和编辑布局。我还希望他们能够包含他们想要的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

橘和柠 2024-10-28 06:54:23

看起来现在这样做是可能的:

class MyDrop < Liquid::Drop

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

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

end

It seems that this is possible now when done this way:

class MyDrop < Liquid::Drop

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

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

end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文