ruby-on-rails - 为什么我的表单身份验证辅助方法会导致整个提交输入类型从页面中删除?
问题描述
我编写了一个辅助方法,它将输出 HTML 以验证表单:
module ApplicationHelper
def auth_token
"<input type=\"hidden\" name=\"authenticity_token\" value=\"#{form_authenticity_token}\"".html_safe
end
end
我尝试在 application.html.erb 的这部分 HTML 代码中使用它:
<% if logged_in? %>
<p>Logged In</p>
<form action="<%= session_url %>" method="POST">
<input type="hidden" name="_method" value="DELETE">
<%= auth_token %>
<input type="submit" value="Logout">
</form>
<% else %>
<p>Logged out</p>
<form action="<%= new_session_url %>" method="GET">
<input type="hidden" name="authenticity_token" value="<%= form_authenticity_token %>">
<input type="submit" value="Login">
</form>
<% end %>
我一次只测试一个按钮,所以目前只有注销按钮使用这个辅助方法!但是,一旦我登录,注销按钮就不会显示(但会出现段落文本)。当我明确地输入表单身份验证输入标签而不是使用我的辅助方法时,它就可以工作了。我仔细检查了我的辅助方法和 erb 标签,对我来说它看起来是正确的。关于为什么会发生这种情况的任何想法?
解决方案
>
您的辅助方法中缺少,请>
在字符串末尾添加。
module ApplicationHelper
def auth_token
"<input type=\"hidden\" name=\"authenticity_token\" value=\"#{form_authenticity_token}\">".html_safe
end
end
推荐阅读
- java - Jsoup - 自定义元素避免格式化
- ruby - 我在使用 Parser gem 在 Ruby 中创建静态代码分析器时无法封装代码
- c# - EF Core 实体上 DDD 上的业务请求实现
- vue.js - [Vue 警告]:未知的自定义元素:
- 您是否正确注册了组件?对于递归组件, - excel - 如何粘贴来自不同工作簿的范围?
- c# - ASP NET 5 - 使用身份时忽略自定义身份验证方案处理程序
- ios - 使用 MAC 终端上传 .dsym 文件时出现问题
- python - Python 等高线图中的最大值
- firebase - 错误:hosting:channel:deploy 不是 Firebase 命令
- python - 将 PyInstaller 用于带有 Matplotlib 的脚本后出错