问题
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