Rails form_authenticity_token不返回任何内容session[:_csrf_token]返回实



长话短说,我手动使用它,因为我必须用纯html创建表单。

Im使用部分(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]}

奇怪的是,我是从那里得到的

有人知道发生了什么事吗?

我认为您引用的文档暗示此方法form_authenticity_token()只能在控制器中访问(class: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}

相关内容

最新更新