Rails - How to add CSRF Protection to forms created in javascript?

后端 未结 5 998
悲&欢浪女
悲&欢浪女 2020-12-04 14:37

I\'m using backbone.js and it works great. but the forms I\'m creating as a javascript template lacks the rails csrf protection token. How do I add it to templates I\'m crea

5条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-04 14:57

    Best way I solved this, inside the form:

    <%= hidden_field_tag :authenticity_token, form_authenticity_token %>
    

    Update:

    It looks like the form_authenticity_token is private for controllers in the newer rails versions.

    If that's the case for you, what I suggest is: declare a variable in a controller like: @form_token = form_authenticity_token and use it in the view you are looking for.

提交回复
热议问题