提交表单需要接收服务器命名为authenticity_token的令牌,并且需要一个只对发送和接收服务器已知的字符串:
<input id="authenticity_token" name="authenticity_token" type="hidden" value="ac513de198a9f0536df5003fb5ba3122d1ee53d5" />如果调用实例或全局变量,则该值呈现。但是,即使控制器是用以下过滤器设置的:
skip_before_action :verify_authenticity_token, only: [:reservation]无论我是否尝试
<%= form_tag('https://test.webten.net/form') do %>
<%= hidden_field_tag :authenticity_token, @ppayment.authenticity_token, id: "authenticity_token" %>或
<%= tag(:input, type: "hidden", name: request_forgery_protection_token.to_s, value: @ppayment.authenticity_token) %>或
<input id="" name="authenticity_token" type="hidden" value="ac513de198a9f0536df5003fb5ba3122d1ee53d5" />Rails最后使用自己的会话设置值压缩每个值,并呈现:
<input type="hidden" name="authenticity_token" id="authenticity_token" value="ydi5En1ywUkN5VsYIBXu6JTbQmXtwxNhpKlyjbbLi3RdvCc+A59EdDZvroGsGFplAAE5ATLcSqw25LVQkyPtKw==">
<input type="hidden" name="authenticity_token" value="ydi5En1ywUkN5VsYIBXu6JTbQmXtwxNhpKlyjbbLi3RdvCc+A59EdDZvroGsGFplAAE5ATLcSqw25LVQkyPtKw==">
<input id="authenticity_token" name="authenticity_token" type="hidden" value="ydi5En1ywUkN5VsYIBXu6JTbQmXtwxNhpKlyjbbLi3RdvCc+A59EdDZvroGsGFplAAE5ATLcSqw25LVQkyPtKw==">如何在此控制器操作中重写rails默认行为?
发布于 2018-03-16 03:50:12
这个答案是对前面的答案的补充。它解决了这个问题,但以不雅的方式解决了这个问题。
各种可能的解决办法似乎是不完整的/毫无意义的
form_authenticity_token是一个视图助手,它返回当前会话的真实性令牌。太晚了,不能对此采取行动,因为布局可能已经调用了。protect_from_forgery,用于出于内部目的忽略它。还为时已晚,因为会话已经发布了它。authenticity_token: 'external_token'时不正确Aaron Breckenridge的回答提供了一个线索:“您可能有一些JS覆盖了真实性令牌",得到了本帖的支持。因此,人们可以搜索应用程序目录的内容,但仍然找不到JS处理程序.例如,当jquery安装到gem时。根据观察到的行为,每次遇到name='authenticity-token'并根据元标记设置值时(逻辑。为什么同一页上有两个不同的标记)--正确地完成它的工作!
从字面上讲,决不能在上游产生这一令牌。因此,修改布局的标题部分:
<% unless request.path == "/controller/action" %>
<%= csrf_meta_tags %>
<% end %>是个可行的解决方案。这种解决方案在多条路径上容易受到污染。另一个需要多个布局处理..。因此,在“kludgey”下归档。(愿意招待更好的人!)
发布于 2018-03-15 20:50:23
在默认情况下,您必须重写:authenticity_token调用中的form_tag参数,以防止Rails添加会话的真实性令牌:
<%= form_tag('https://test.webten.net/form', authenticity_token: 'ac513de198a9f0536df5003fb5ba3122d1ee53d5') do %>或者:
<%= form_tag('https://test.webten.net/form', authenticity_token: false) do %>
<%= hidden_field_tag 'authenticity_token', 'ac513de198a9f0536df5003fb5ba3122d1ee53d5' %>如果这不起作用,您可能有一些JS覆盖了从元标记中提供的真实性令牌。搜索‘csrf-令牌’在您的JS。
有关详细信息,请参阅Helpers.html#forms到--外部资源。
https://stackoverflow.com/questions/49303308
复制相似问题