【发布时间】:2011-11-14 19:46:15
【问题描述】:
在 Rails 中,我在 ApplicationController 中添加了几个rescue_from 处理程序。它们工作正常,除了看起来会话丢失了,因为下一个请求总是被重定向到登录页面。
应用控制器:
rescue_from ActiveRecord::RecordNotFound, :with => :handle_record_not_found
rescue_from ActiveRecord::RecordInvalid, :with => :handle_record_invalid
rescue_from ApplicationSecurityError, :with => :handle_security_error
rescue_from ApplicationError, :with => :handle_application_error
其中一个处理程序:
def handle_application_error(error)
logger.warn "#{error.class} #{error.user_safe_message} #{error.debug_info}, user #{@current_user.id}"
render :json => {:errors => {:base => error.user_safe_message}, :status => :ok}
end
有没有办法在不丢失会话的情况下使用这些处理程序? 我检查了 Firebug,它肯定是服务器端的,因为 jQuery 从那个比以前更短的错误请求中获得了不同的 cookie,所以我认为我确实失去了会话。
【问题讨论】:
-
您可能需要更具体一些。在什么情况下你会看到问题?如果是 Ajax 请求,是否包含 CSRF 令牌?
-
是的,这就是问题所在。我没有包括他们。我使用 _method:'put' 指定了更新对象的 POST 请求。在我编辑我的问题之前提到了 jQuery.ajax。哎呀。
标签: ruby-on-rails error-handling