![](/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.