简体   繁体   中英

Get extra information from ActiveRecord::RecordNotFound in Rails

I handle RecordNotFound error in my application_controller.rb as follow:

  rescue_from ActiveRecord::RecordNotFound, :with => :record_not_found

  private 

   def record_not_found
     flash[:error] = "Oops, we cannot find this record"
     redirect_to :back
   end

But I would like to get more information, such as class/table name of which record was not found. How should I go about it?

Thank you.

You can define a parameter in your rescue handler and exception will be passed there.

def record_not_found exception
  flash[:error] = "Oops, we cannot find this record"
  # extract info from exception

  redirect_to :back
end

If you can't get that info from the exception, you're out of luck (I think).

I had some success with this:

# in app/controllers/application_controller.rb

rescue_from ActiveRecord::RecordNotFound, with: :record_not_found

def record_not_found exception
  result = exception.message.match /Couldn't find ([\w]+) with 'id'=([\d]+)/
  # result[1] gives the name of the model
  # result[2] gives the primary key ID of the object that was not found
end

HTH

EDIT: Whitespace error removed at the end of the Regex. Thanks to the commenters. :)

Say for example,

begin
@user = User.find(params[:id])
rescue ActiveRecord::RecordNotFound
  flash[:notice] = "#No such record in User for id :: {params[:id]} on #{action_name}"
end

UPDATE

flash[:notice] = t('flash.recordnotfound',:class_name => self.class.name, :column_name => params[:id], :action_name => action_name)

Now in your config/locales/en.yml (this would help translate, refer to i18n here )

flash:
  recordnotfound:  "Sorry, no record od %{column_name} in class %{class_name} was found on you action %{action_name}"

If you do not want to use locales just put up this information in flash[:notice] itself.

More dynamic ?

Write a function and use the same flash [:notice] there. Wont hurt at all.

want more data ?

Heres a quick solution, i always <%= params%> in my views to know easily whats going and whats coming. You can then open your rails console and play along with different actions and so on.

user = User.new
user.save
user.errors.messages

All of this is good enough data, i think.

Good luck.

Once you instantiate the model, you can check something like.

human = Human.new
human.errors

Check this in rails console so you could play with it and use it in the main controller.

rescue_from ActiveRecord::RecordNotFound do |exception|
    raise ActiveRecord, exception.message, exception.backtrace
end

EDIT Make sure you the application controller extends the base.

class ApplicationController < ActionController::Base
 rescue_from Exception, :with => :record_not_found
  private 

   def record_not_found(e)
     flash[:error] = "Oops, we cannot find this record" + e.message
     redirect_to :back
   end
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM