简体   繁体   中英

ActionController::InvalidAuthenticityToken in ruby on rails

I have a popup box name "Delete My account" where I am getting the password and delete account this password, below is the button where I click to open popup box:

<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>

by using this javascript, open popup box:

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

And below is he popup form:

<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

And below is javascript for submitting the form:

<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>

And problem is that when I click submit button on the popup box, I am getting an error below:

ActionController::InvalidAuthenticityToken

To resolve this issue I add authenticity_token: "true" in form like below:

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

After that still I am getting another error below:

ActionController::InvalidAuthenticityToken

Kindly suggest me, what should I do to resolve this issue, waiting for your reply. Thanks.

According to this question , the problem is typically with the CSRF tags in Rails

They suggest including the csrf meta tags in your application layout. Do you have this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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