【发布时间】:2016-11-03 05:18:12
【问题描述】:
我的 HEAD 标记中有 <%= csrf_meta_tags %>,但我手动创建了表单,所以 authenticity_token 隐藏字段没有插入到我的表单中。
所以我手动添加了authenticity_token:
<input name="authenticity_token" value="<%= form_authenticity_token %>" type="hidden">
当我发送我网站的默认联系表单(用户未登录)时,处理它的操作应验证令牌并允许/拒绝请求。但事实并非如此:我使用 jQuery 清空字段并提交表单但没有抛出错误或异常。
我在application_controller.rb 中得到了这一行protect_from_forgery with: :exception 并将其放入扩展application_controller 并具有响应联系表单的操作的foo_controller.rb 中。
那么我错过了什么?我需要做什么才能在后端验证此表单?
非常感谢!
【问题讨论】:
-
你不需要
protect_from_forgery with: :exceptioninfoo_controller.rb只要应用程序控制器有它。您的设置通常是正确的,但如果没有看到表单/javascript 代码,很难说出更多信息,请添加它。
标签: ruby-on-rails ruby-on-rails-4 csrf authenticity-token