Liquid 插件错误:“form_for 未定义”

发布于 2024-07-25 10:33:53 字数 173 浏览 5 评论 0 原文

我在我的应用程序中使用液体插件。 在使用诸如 form_forcheck_box_tag 之类的 Rails 辅助方法时,我收到 错误 form_for not Define..

有谁知道如何使用 Rails 辅助方法通过液体插件?

I am using liquid plugin in my app.
While using rails helper methods like form_for, check_box_tag, I am getting an error form_for not defined..

Has anybody know how to use rails helper methods through liquid plugin?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

半仙 2024-08-01 10:33:53

我不久前刚刚处理过这个问题 - 如果您想更好地了解如何扩展普通过滤器(包括您可能需要的辅助方法),我发现这个 Railscast 非常有帮助: http://railscasts.com/episodes/118-liquid

基本上,您需要设置自己的过滤器文件并包含帮助程序您想要使用的模块,然后添加使用该帮助器的方法(过滤器)。 这并不难,只需一秒钟即可设置。 在 Ryan 的示例中,他在 lib 中设置了一个名为 LiquidFilters 的模块,包括他想要的数字助手,并将其设置为使用货币过滤器,如下所示:

# lib/liquid_filters.rb
module LiquidFilters
  include ActionView::Helpers::NumberHelper

  def currency(price)
    number_to_currency(price)
  end
end

然后您所要做的就是记住何时解析要添加的液体内容:过滤器 => [LiquidFilters](获取您想要使用的过滤器模块的数组),它应该自动选择它。 此方法还确保如果您想要设置更多自定义过滤器或修改辅助过滤器,您可以轻松直观地执行此操作。

I was just dealing with this not too long ago - if you want a better understanding of what it takes to extend the normal filters (including helper methods you may want) I found this Railscast very helpful: http://railscasts.com/episodes/118-liquid

Basically, you'll need to set up your own filter file and include the helper modules you want to use, then add a method (filter) which uses that helper. It's not hard, just takes a second to setup. In Ryan's example he sets up a module in lib called LiquidFilters, includes the number helper he wanted and set it up to use a currency filter like so:

# lib/liquid_filters.rb
module LiquidFilters
  include ActionView::Helpers::NumberHelper

  def currency(price)
    number_to_currency(price)
  end
end

Then all you have to do is remember when you're parsing the liquid content to add :filters => [LiquidFilters] (takes an array of filter modules you want to use) and it should pick it up automatically. This method also makes sure that if you want to set up any more custom filters or modify the helper filters you have an easy and intuitive place to do that.

坠似风落 2024-08-01 10:33:53

您可以直接使用html代码,例如,不使用link_to(不起作用!),在liquid模板中,您可以使用liquid标签编写html代码:

<a href="{link}">{{link_name}}</a>" 

You can use directly html code, as instance, instead of use link_to (doesn't work!), inside the liquid template, you can write html code with liquid tags:

<a href="{link}">{{link_name}}</a>" 
爱,才寂寞 2024-08-01 10:33:53

有人知道如何通过 Liquid 插件使用 Rails 辅助方法吗?

Liquid 有自己的助手,称为过滤器。 请参阅 https://github.com/Shopify/liquid/wiki/Liquid-for -设计师

您可以决定在将值传递给 Liquid 之前应用帮助程序,也可以扩展 Liquid 注册您自己的过滤器(请参阅 https://github.com/Shopify/liquid/wiki/Liquid-for-Programmers)。

如果您将 Liquid 注册为 Rails 模板处理程序,Liquid 会尝试使用您的助手作为过滤器。

但是,您需要使用 Liquid 语法。

{{ 'This is a long section of text' | truncate: 3 }}

不是

{{ truncate('This is a long section of text', 3) }}

Has anybody know how to use rails helper methods through liquid plugin?

Liquid has its own helpers, called filters. See https://github.com/Shopify/liquid/wiki/Liquid-for-Designers.

You can either decide to apply the helper before passing the value to liquid or extend liquid registering your own filters (see https://github.com/Shopify/liquid/wiki/Liquid-for-Programmers).

If you register liquid as Rails template handler, Liquid tries to use your helpers as filters.

However, you need to use the Liquid syntax.

{{ 'This is a long section of text' | truncate: 3 }}

Not

{{ truncate('This is a long section of text', 3) }}
青春有你 2024-08-01 10:33:53

看一下 Liquid 的这个名为“Clots”的项目。 据说它解决了这个问题(我个人没有使用过它)。

http://github.com/ludicast/clots/tree/master

Take a look at this project for Liquid called "Clots". It supposedly addresses this issue (I haven't personally used it).

http://github.com/ludicast/clots/tree/master

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