My setup: Rails 3.0.9, Ruby 1.9.2
I am using Cancan to authorize a controller action. If the user specify a missing id, then I have the following code
application_controller.rb
rescue_from ActiveRecord::RecordNotFound do |exception|
flash[:alert] = "Oops, I cannot find this record, please try again."
respond_to do |format|
format.html { redirect_to root_url }
end
end
What I would like is for the above code to set the flash message to something like
flash[:alert] = "Oops, I cannot find this person, please try again."
“person” in this case could be any of the model, e.g, if the user tried to access a missing id for a country, it should say
flash[:alert] = "Oops, I cannot find this country, please try again".
You get the idea. I’m thinking I should be able to grab the originating call and controller, does anyone know how to do this or has a better way to do it?
one thing you could do is to parse
exception.messagewhich usally contains a string looking like this“Couldn’t find Image with ID=03 [WHERE images.state = ‘published’]”
but I would use the
paramsobject to access the controller and action causing the errorlike this:
cheers