neo*_*nde 20 ajax ruby-on-rails rescue ruby-on-rails-3
在我的Rails 2.3.8应用程序中,我有一个例外的rescue_from代码,它在javascript操作期间抛出:
rescue_from ::Exception, :with => :show_js_errors
...
def show_js_errors exception
  if request.format == :js
    flash[:error] = 'some error occured'
    render :update do |page|
      page.redirect_to({:controller => '/home', :action => :index})
    end
  else
    # use default error handling for non-JS requests
    rescue_action_without_handler(exception)
  end
end
Run Code Online (Sandbox Code Playgroud)
因此,如果ajax调用遇到错误,我的用户会收到错误消息.在Rails 3中,我不能简单地调用默认的错误处理,因为"without_handler"方法不再存在.
更新 doh
经过3个小时的搜索,我发布了这个,但发布后仅30分钟我就找到了解决方案.
只是重新提出异常.
由于您处于错误处理状态,因此不会对此异常进行进一步处理.
只需重新引发异常即可。
def show_js_errors exception
  if request.format == :js
    flash[:error] = 'some error occured'
    render :update do |page|
      page.redirect_to({:controller => '/home', :action => :index})
    end
  else
    raise # <<
  end
end
Run Code Online (Sandbox Code Playgroud)
http://simonecarletti.com/blog/2009/11/re-raise-a-ruby-exception-in-a-rails-rescue_from-statement/同意:
Run Code Online (Sandbox Code Playgroud)rescue_from ActiveRecord::StatementInvalid do |exception| if exception.message =~ /invalid byte sequence for encoding/ rescue_invalid_encoding(exception) else raise end end[...]该异常被正确地重新抛出,但标准 Rails 救援机制未捕获该异常,并且未呈现标准异常页面。