如何从液体标签访问Rails控制器? [英] How to access the Rails controller from a liquid tag?
问题描述
我希望能够在液体模板上渲染Rails的CSRF内容.我发现有两个页面为我提供了一些线索:
I want to be able to render Rails' CSRF stuff on a liquid template. I found two pages that give me some clues:
- http://robots.thoughtbot.com/post/159806314/液体中的自定义标签
- https://github.com/locomotivecms/engine/blob/v2.0.0.rc12/lib/locomotive/liquid/tags/csrf.rb
- http://robots.thoughtbot.com/post/159806314/custom-tags-in-liquid
- https://github.com/locomotivecms/engine/blob/v2.0.0.rc12/lib/locomotive/liquid/tags/csrf.rb
我的问题是,thoughtbot链接没有首先说明如何获取控制器的注册.然后,查看locomotivecms源,我可以看到模板已在新上下文中注册,但这是在帮助程序中完成的.我认为这种方法不适合我,因为我希望能够仅在我的.liquid
文件中调用标签{% csrf_meta_tag %}
.
My problem is that the thoughtbot link does not explain how to get register the controller in the first place. Then, looking at the locomotivecms source, I can see that the template is registered with a new context, but this is done in the helper. I don't think this approach would be appropriate for me because I want to be able to just call the tag {% csrf_meta_tag %}
in my .liquid
file.
我的方法可行吗?如果我处在正确的轨道上,那么我将如何始终将控制器与上下文一起注册?如果我走的路不正确,该怎么办?
Is my approach possible? If I am on the right track, how would I then always register the controller along with the context? If I'm not on the right track, how can I go about it?
谢谢!
推荐答案
I have the same problem and I found the solution. Using this code, you can add theese tags to your template:
{% csrf_meta %}
{% csrf_param %}
这篇关于如何从液体标签访问Rails控制器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!