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

后端 未结 5 1005
悲&欢浪女
悲&欢浪女 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 15:03

    You can prepend the csrf token to every form that uses 'post' or 'delete'. Here it is in coffeescript:

    $ -> 
      for f in $("form")
        if f.method == 'post' or f.method == 'delete'
          $(f).prepend("")
    

    Make sure you have <%= csrf_meta_tags %> in your layout. It should already be in the standard 'application' layout, but add it if you're using a different layout.

提交回复
热议问题