如何从液体标签访问Rails控制器? [英] How to access the Rails controller from a liquid tag?

查看:58
本文介绍了如何从液体标签访问Rails控制器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够在液体模板上渲染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/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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆