Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 8th, 2012  |  syntax: None  |  size: 1.27 KB  |  hits: 13  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Get extra information from ActiveRecord::RecordNotFound in Rails
  2. rescue_from ActiveRecord::RecordNotFound, :with => :record_not_found
  3.  
  4.   private
  5.  
  6.    def record_not_found
  7.      flash[:error] = "Oops, we cannot find this record"
  8.      redirect_to :back
  9.    end
  10.        
  11. begin
  12. @user = User.find(params[:id])
  13. rescue ActiveRecord::RecordNotFound
  14.   flash[:notice] = "#No such record in User for id :: {params[:id]} on #{action_name}"
  15. end
  16.        
  17. flash[:notice] = t('flash.recordnotfound',:class_name => self.class.name, :column_name => params[:id], :action_name => action_name)
  18.        
  19. flash:
  20.   recordnotfound:  "Sorry, no record od %{column_name} in class %{class_name} was found on you action %{action_name}"
  21.        
  22. user = User.new
  23. user.save
  24. user.errors.messages
  25.        
  26. def record_not_found exception
  27.   flash[:error] = "Oops, we cannot find this record"
  28.   # extract info from exception
  29.  
  30.   redirect_to :back
  31. end
  32.        
  33. human = Human.new
  34. human.errors
  35.        
  36. rescue_from ActiveRecord::RecordNotFound do |exception|
  37.     raise ActiveRecord, exception.message, exception.backtrace
  38. end
  39.        
  40. class ApplicationController < ActionController::Base
  41.  rescue_from Exception, :with => :record_not_found
  42.   private
  43.  
  44.    def record_not_found(e)
  45.      flash[:error] = "Oops, we cannot find this record" + e.message
  46.      redirect_to :back
  47.    end
  48. end