繁体   English   中英

在Ruby on Rails中的ActionController :: InvalidAuthenticityToken

[英]ActionController::InvalidAuthenticityToken in ruby on rails

我有一个弹出框名称“ Delete My account”,在其中我要获取密码并删除该密码帐户,下面是单击打开弹出框的按钮:

<table align="center" width="100%">
      <tr>
         <td>    
            <div class="btn_delmyaccount" align="center">
                <a id="DeleteMyAccount" class="buttonSearch">Delete My Account</a>
            </div>
         </td>
      </tr>
</table>

通过使用此javascript,打开弹出框:

<script type="text/javascript">
                var e2=document.getElementById("DeleteMyAccount");
                e2.onclick = show_dialog3;
                function show_dialog3() {
                $( "#dialog2" ).dialog();
                }
</script>

下面是他的弹出表单:

<div id="dialog2" title="Delete My Account" hidden="true">
                <%= form_tag({ controller: "settings", action: "delete_my_account"}, remote: "true" ) do |f| %>
                    <table style="text-align:center; vertical-align:top;">
                        <tr>
                            <td>
                                <div>
                                    <%= label_tag(:password, "Password") %>
                                    <%= password_field :tf_password, :placeholder => "Password" %>
                                </div>
                            </td>
                        </tr>
                    </table>
                    <table align="center" width="63%">
                        <tr>
                            <td style="text-align:right;">
                                <%= submit_tag 'Submit', :id => "_button1" %>
                            </td>
                        </tr>
                    </table>
                <% end %>
</div

以下是用于提交表单的javascript:

<script type="text/javascript">
$(document).ready(function() {
    // '_button' is the Id of your submit button
    $("#_button1").click(function() {            
      $(this).closest("form").submit();
      $("#dialog2").dialog("close");
    });
});
</script>

问题是,当我单击弹出框上的“提交”按钮时,出现以下错误:

ActionController::InvalidAuthenticityToken

要解决此问题,我以如下形式添加authenticity_token: "true"

<%= form_tag({ controller: "settings", action: "delete_my_account" , authenticity_token: "true" }, remote: "true" ) do |f| %>
<% end %>

之后,我仍然在下面出现另一个错误:

ActionController::InvalidAuthenticityToken

请建议我,如何解决此问题,等待您的答复。 谢谢。

根据这个问题 ,问题通常是与Rails中的CSRF标签有关

他们建议在您的application布局中包含csrf元标记。 你有这个吗:

#app/views/layouts/application.html.erb
<%= csrf_meta_tags %>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM