Ruby on Rails自动事务过滤器及相应的错误处理

来源:互联网 发布:手绘软件painter mac 编辑:程序博客网 时间:2024/05/16 14:26

 

  1. around_filter :around_action_filter  
  2.       
  3. protected  
  4.     def transaction  
  5.         ret = true  
  6.           
  7.         ActiveRecord::Base.transaction do  
  8.             begin  
  9.                 yield if block_given?  
  10.             rescue Exception => ex  
  11.                 set_notice(ex.message)  
  12.                 ret = ex.message  
  13.                 raise ActiveRecord::Rollback, ex.message  
  14.             end  
  15.         end  
  16.           
  17.         return ret  
  18.     end  
  19.       
  20.     def around_action_filter  
  21.         return yield if request.get?          
  22.         redirect_to(:controller => "error_display":action => "error_notice"if   
  23.             transaction { yield if block_given? } != true  
  24.     end  

 

这是写在ApplicationController里的处理代码。其中的set_notice只是将flash[:notice]设置一下,以便在error_notice页面显示。