gpt4 book ai didi

ruby-on-rails - Rails form_authenticity_token 不返回任何 session [:_csrf_token] returns the actual token

转载 作者:太空宇宙 更新时间:2023-11-03 17:54:02 25 4
gpt4 key购买 nike

长话短说,我手动使用它,因为我必须用纯 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}

关于ruby-on-rails - Rails form_authenticity_token 不返回任何 session [:_csrf_token] returns the actual token,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17996270/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com