ActionController::InvalidAuthenticityToken 在 ActiveAdmin::Devise::SessionsController#create

ActionController::InvalidAuthenticityToken in ActiveAdmin::Devise::SessionsController#create

我在 Rails 5 Api 应用程序上使用 Ruby,并进行了修改以启用 Active Admin。 到现在为止一切都很好。我不记得在应用程序中做过任何更改,但现在,如果我在浏览器上删除 cookies 等,我将无法登录到活动的管理应用程序,我得到的是这个错误:

我试图在应用程序控制器中添加两者

protect_from_forgery :with => :exception

protect_from_forgery :with => :null_session

但 none 有效。 这是我的应用程序控制器:

class ApplicationController < ActionController::Base
  # protect_from_forgery :with => :exception
  before_action :configure_permitted_parameters, if: :devise_controller?

  protected
  def configure_permitted_parameters
    attributes = [:name]
    devise_parameter_sanitizer.permit(:sign_up, keys: attributes)
  end
end

我不知道是什么原因造成的,也不知道如何解决。 提前致谢。

现在可以了。在我重新启动计算机并添加行后:

protect_from_forgery prepend: true, with: :exception

而不是在应用程序控制器中,它起作用了。

当我在不重启服务器的情况下从一个分支切换到另一个分支时,我经常遇到这种情况。

每次重新启动 rails 服务器都对我有用 :)

在我的上下文中,我有一个自定义管理页面,为了解决这个问题,我放置了一个 authenticity_token 隐藏字段。如下所示:

form method: :post, action: admin_templates_save_template_path do |f|
   f.label "label", for: :base_proposal_url
   f.input id: :base_proposal_url, name: :base_proposal_url

   ### field to handle authenticity token
   f.input type: :hidden, name: :authenticity_token

   f.button "Save", type: :submit
end