how to generate AuthenticityToken on rails

前端 未结 4 1091
说谎
说谎 2021-01-03 17:26

I build the form tag by myself and when I post the form to server it give me a InvalidAuthenticityToken error, so I want to know how to add it in my own in curr

4条回答
  •  星月不相逢
    2021-01-03 18:19

    To generate the token you have to use the method: form_authenticity_token as it was correctly noted by @flitzwald. Since it is rediced in a active controller's concern, you must include the module into a controller expclicitly before using as follows:

    include ActionController::RequestForgeryProtection
    
    # use
    
    def set_csrf_header
      response.headers['X-CSRF-Token'] = form_authenticity_token
    end
    

提交回复
热议问题