ActionController :: SessionsController#destroy中的InvalidAuthenticityToken

时间:2017-11-04 13:36:42

标签: ruby-on-rails

退出我的Rails应用时出现上述错误。我看到其他人在使用Devise时遇到了同样的错误,但是我没有使用那个宝石。这是我的SessionsController.rb:

    class SessionsController < ApplicationController

  def new

  end

  def create 
    chef = Chef.find_by(email: params[:session][:email].downcase)
    if chef && chef.authenticate(params[:session][:password])
      session[:chef_id] = chef.id
      cookies.signed[:chef_id] = chef.id
      flash[:success] = "You have successfully logged in."
      redirect_to chef
    else
      flash.now[:danger] = "There was something wrong with your login information."
      render 'new'
    end
  end

  def destroy
    session[:chef_id] = nil
    flash[:success] = "You have logged out."
    redirect_to root_path
  end

end

非常感谢您的帮助。

这是我的退出按钮,它是导航栏的一部分:

<li><%= link_to "Log out", logout_path, method: :delete %></li>
        </ul>
      </li>
      <% else %>
        <li><%= link_to "Log in", login_path %></li>
      <% end %>

这是我的_navigation partial的一大部分,其中包含注销按钮:

  

<% if logged_in? %><%#Changes drop-down menu options if logged in.%>
>           <li class="dropdown">
>             <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false"><%=
> current_chef.chefname.titleize %>'s Profile <%= "Admin" if
> current_chef.admin? %><span class="caret"></span></a>
>             <ul class="dropdown-menu">
>               <li><%= link_to "View your profile", chef_path(current_chef) %></li>
>               <li><%= link_to "Edit your profile", edit_chef_path(current_chef) %></li>
>               <li><a href="#">Something else here</a></li>
>               <li role="separator" class="divider"></li>
>               <li><%= link_to "Log out", logout_path, method: :delete%></li>
>             </ul>
>           </li>
>           <% else %>
>             <li><%= link_to "Log in", login_path %></li>
>           <% end %>

2 个答案:

答案 0 :(得分:2)

我认为您在布局中缺少csrf_meta_tag语句(请记住,它需要jquery_ujs / jquery-rails才能正常工作)。尝试添加它并检查它是否有效

答案 1 :(得分:0)

ActionController::InvalidAuthenticityToken错误与Devise gem无关,请protect_from_forgery查看Cannot add foreign key constraint。查看应用程序控制器内部。