Ruby on Rails automatic transaction filter and corresponding error handling

Source: Internet
Author: User
Tags ruby on rails

 

  1. Around_filter: around_action_filter
  2. Protected
  3. Def transaction
  4. Ret = true
  5. Activerecord: Base. Transaction do
  6. Begin
  7. Yield if block_given?
  8. Rescue exception => ex
  9. Set_notice (ex. Message)
  10. Ret = ex. Message
  11. Raise activerecord: rollback, Ex. Message
  12. End
  13. End
  14. Return ret
  15. End
  16. Def around_action_filter
  17. Return yield if request. Get?
  18. Redirect_to (: controller => "error_display",: Action => "error_notice") if
  19. Transaction {yield if block_given? }! = True
  20. End

Around_filter: around_action_filter </P> <p> protected <br/> def transaction <br/> ret = true </P> <p> activerecord: base. transaction DO <br/> begin <br/> yield if block_given? <Br/> rescue exception => ex <br/> set_notice (ex. message) <br/> ret = ex. message <br/> raise activerecord: rollback, Ex. message <br/> end </P> <p> return ret <br/> end </P> <p> def around_action_filter <br/> return yield if request. get? <Br/> redirect_to (: controller => "error_display",: Action => "error_notice") if <br/> transaction {yield if block_given? }! = True <br/> end

 

This is the processing code written in applicationcontroller. The set_notice only Sets flash [: Notice] to be displayed on the error_notice page.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.