长话短说,我手动使用它,因为我必须用纯 html 创建表单。
我正在使用部分(在 haml 中)
%input{name: "authenticity_token", type: "hidden", value: form_authenticity_token}
这会输出一个空白的输入框:
<input name='authenticity_token' type='hidden'>
但这可以正常工作:
%input{name: "authenticity_token", type: "hidden", value: session[:_csrf_token]}
奇怪的是,我从 docs 得到了这个
有人知道发生了什么事吗?
我认为您引用的文档暗示此方法 form_authenticity_token()
只能在 Controller (类:ActionController::RequestForgeryProtection)中访问。
您应该直接使用session[:_csrf_token]
或者您可以设置一个共享变量:
# in controller
def my_action
@auth_token_form = form_authenticity_token
end
# in view
%input{name: "authenticity_token", type: "hidden", value: @auth_token_form}
我是一名优秀的程序员,十分优秀!