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.
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. :)
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With