'Liquid plugin error: "form_for not defined"
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?
Solution 1:[1]
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.
Solution 2:[2]
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>"
Solution 3:[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) }}
Solution 4:[4]
Take a look at this project for Liquid called "Clots". It supposedly addresses this issue (I haven't personally used it).
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | PJ. |
| Solution 2 | Alessandro De Simone |
| Solution 3 | Ramon Tayag |
| Solution 4 | Barry Gallagher |
