![](/img/trans.png)
[英]ActionController::InvalidAuthenticityToken (ActionController::InvalidAuthenticityToken): Rails 5
[英]Intermittent Rails 5 ActionController::InvalidAuthenticityToken
上下文:一个在Heroku上托管的量产的Rails应用,拥有大约800个用户。
由于某种原因,我看到一些用户遇到错误:
ActionController::InvalidAuthenticityToken
[GEM_ROOT]/gems/actionpack-5.1.4/lib/action_controller/metal/request_forgery_protection.rb:195
对于POST /students/:id/registrations
请求。
它是间歇性的,很少有用户遇到此错误。
客户端是iPad上的Safari 11.0。
ApplicationController中:
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
before_action :authenticate_user!, unless: :devise_controller?
before_action :restrict_from_students, unless: :devise_controller?
# ...
end
RegistrationsController:
class RegistrationsController < ApplicationController
skip_before_action :restrict_from_students, only: :create
# ...
end
是否存在某种情况(重新发布请求,验证超时但提交,缺少JS)会导致这种情况? 我似乎无法复制它。
感谢橡皮鸭,我转载了这个问题。
此处的解决方案是使用rescue_from
可能将用户重定向到登录页面。
谢谢橡皮鸭!
我有一个类似的问题。
在应用程序控制器中使用rescue_from
,并在有用通知的地方重定向。 在我的情况下,我尝试将用户重定向到他们要重新尝试其操作的位置,或者将其重定向到主页。
导轨5的示例:
class ApplicationController < ActionController::Base
rescue_from ActionController::InvalidAuthenticityToken,
with: :handle_invalid_token
def handle_invalid_token
redirect_back fallback_location: root_path,
notice: 'Stale session detected'
end
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.