gpt4 book ai didi

ruby-on-rails - Rails 4.如何向通过局部渲染的表单添加authenticity_token?

转载 作者:行者123 更新时间:2023-12-03 13:41:47 25 4
gpt4 key购买 nike

在我的rails应用程序的所有页面的头部,都有以下2个meta标签:

<meta name="csrf-param" content="authenticity_token" />
<meta name="csrf-token" content="027GUZBeEkmv..." />

在未使用部分显示的表单上,存在一个隐藏的 authenticity_token字段
<input type="hidden" name="authenticity_token" value="D5TddQruJppDD3..." />

但是,如果我只是这样加载表单,则此字段会丢失:
<%= render 'shared/comment_form' %>

这是预期的行为吗?我应该手动添加一个 authenticity_token,如果是的话如何验证它?

编辑:

共享/_comment_form.html.erb
<%= form_for([@post, @comment], :html => { :onsubmit => "validateCommentForm(event)" }, remote:true) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<div class="field">
<%= f.text_area :content, placeholder: "Add to the article. Make it be more" %>
</div>

<%= f.submit "Save", class: "btn btn-info" %>
<% end %>

此外,向该表单添加 <input type="hidden" name="authenticity_token" id="authenticity_token" value="ANYTHING" />仍然可以发布信息并创建新记录...

最佳答案

对于您而言,我们有两种方法可以执行:

  • 在表单选项
  • 中添加 authenticity_token: true
  • 手动将authenticity_token字段添加到表单中,如下所示:
  • <%= hidden_field_tag :authenticity_token, form_authenticity_token -%>

    关于ruby-on-rails - Rails 4.如何向通过局部渲染的表单添加authenticity_token?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36000511/

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