I have an action that searches records based on a url parameter. The url for the action looks something like this:
http://domain.com/records/filter/<filtercode>
If the user enters an incorrect filtercode, I would like to app to
I understand that certain Rails errors such as ActiveRecord::NotFound and ActionController:RoutingError will render 404 in production env. So what I like to do is raise this error when a user enters an invalid filtercode in the url.
Now, my question: what's the ideal error to raise in this type of situation? Is there a list of Rails errors/exceptions in the net?
TypeError is raised whenever an operation is performed on an incorrect/unsupported object type. For example, using the + (addition) operator on a string and an integer value will raise TypeError.
When an error occurs in your program, you may either print a message and use sys. exit(1) to abort the program, or you may raise an exception. The latter task is easy. You just write raise E(message) , where E can be a known exception type in Python and message is a string explaining what is wrong.
You're looking for
ActionController::RoutingError
I like to do this in my application_controller:
def not_found
raise ActionController::RoutingError.new('Not Found')
end
Then in other controllers, say pages#show for example, I can do:
def show
unless @page = Page.find_by_permalink(params[:permalink])
not_found
end
end
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