【问题标题】:ActionController::InvalidAuthenticityToken in ruby on railsruby on rails 中的 ActionController::InvalidAuthenticityToken
【发布时间】:2014-05-11 20:18:31
【问题描述】:

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

<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

请建议我,我该怎么做才能解决这个问题,等待您的回复。 谢谢。

【问题讨论】:

    标签: jquery ruby-on-rails


    【解决方案1】:

    根据to this question,问题通常出在Rails 中的CSRF tags

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

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

    【讨论】:

      猜你喜欢
      • 2018-11-02
      • 2018-11-27
      • 2017-11-13
      • 2015-05-21
      • 2015-11-23
      • 2015-01-10
      • 2016-11-14
      • 2019-12-25
      • 1970-01-01
      相关资源
      最近更新 更多