当用户尝试使用脚本或自动化填写表单时,应用程序控制器会引发错误
“ActionController::InvalidAuthenticityToken”
当有效的真实用户填写表单、关闭浏览器、从浏览器历史记录中重新打开页面并提交表单时,就会发生这种情况。
在这种情况下,我不想使用异常通知器发送异常,并且我还想显示带有刷新的请求消息的模式。
所以我将application_controller修改为
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
rescue_from ActionController::InvalidAuthenticityToken, with: :handle_csrf_error
def handle_csrf_error(exception)
respond_to do |format|
format.js {
render 'invalid_requests/error'
}
format.html {
render text: I18n.t('errors.messages.csrf_error')
}
end
ExceptionNotifier.notify_exception(exception)
end
end
我想让它适用于所有类型的请求。
我添加了对 html & js 请求的响应
但没有得到如何处理 json 请求。
PS > json 请求是从 Web 应用程序发送的,用于加载更多案例,有时会引发异常,所以我要处理这个。
我的 Rails 版本是 4.2