How to access the Rails controller from a liquid tag?

耗尽温柔 提交于 2019-12-06 08:09:47

问题


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

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?

Thanks!


回答1:


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 %}

Reference



来源:https://stackoverflow.com/questions/13437242/how-to-access-the-rails-controller-from-a-liquid-tag

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!