# Handling errors - incorrect value entered in url

**URL:** <https://www.ruby-forum.com/t/handling-errors-incorrect-value-entered-in-url/59074>\
**Category:** Rails\
**Created:** [April 28, 2006, 12:59pm UTC](https://www.ruby-forum.com/t/handling-errors-incorrect-value-entered-in-url/59074 "2006-04-28T12:59:31Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![aftershock](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8e7dd6/32.png) [@aftershock](https://www.ruby-forum.com/u/aftershock)\
**Post date:** [April 28, 2006, 12:59pm UTC](https://www.ruby-forum.com/t/handling-errors-incorrect-value-entered-in-url/59074/1 "2006-04-28T12:59:31Z")

</div>

I have many “edit” actions in my project. If the url is like  
[http://localhost/project/3/edit](http://localhost/project/3/edit) and someone enters  
[http://localhost/project/3333333/edit](http://localhost/project/3333333/edit) the edit action will fails because  
it cannot find a project with id of 3333333.

To combat this I am adding to every action like edit:

rescue  
redirect\_to :action =\> ‘index’  
flash[‘errors’] = “That project cannot be edited.”

This works, however is there a way to do this globally or do I have to  
entered this for every action that could error in this way? I understand  
about using routes to narrow down what URL’s can be typed, but this does  
not escape an error when a value entered is incorrect.

Incidentally, I looked at the Typo code and could not see any error  
checking, not a rescue in sight. How do they do it in typo?

---

<div class="post-metadata">

**Author:** ![aftershock](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8e7dd6/32.png) [@aftershock](https://www.ruby-forum.com/u/aftershock)\
**Post date:** [April 28, 2006, 3:13pm UTC](https://www.ruby-forum.com/t/handling-errors-incorrect-value-entered-in-url/59074/2 "2006-04-28T15:13:25Z")

</div>

On Apr 28, 2006, at 3:59 AM, James W. wrote:

> flash[‘errors’] = “That project cannot be edited.”  
> This works, however is there a way to do this globally or do I have to  
> entered this for every action that could error in this way? I  
> understand  
> about using routes to narrow down what URL’s can be typed, but this  
> does  
> not escape an error when a value entered is incorrect.

You could have a before\_filter call a function that tests that the id  
in your URL is valid. If you also want to verify that the right  
person is attempting to edit it… you could do that there as well.

Hope that helps.

Cheers,

-Robby

Robby R.  
Founder & Executive Director

PLANET ARGON, LLC  
Ruby on Rails Development, Consulting & Hosting

> **[Planet Argon](https://www.planetargon.com)**
>
> Planet Argon is a Ruby on Rails development agency specializing in maintenance and support for existing Rails apps. Based in Portland, OR, Planet Argon has been a member of the Rails community since 2004.

[www.robbyonrails.com](http://www.robbyonrails.com)

+1 503 445 2457  
+1 877 55 ARGON [toll free]  
+1 815 642 4968 [fax]

---

<div class="post-metadata">

**Author:** ![aftershock](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8e7dd6/32.png) [@aftershock](https://www.ruby-forum.com/u/aftershock)\
**Post date:** [April 28, 2006, 3:19pm UTC](https://www.ruby-forum.com/t/handling-errors-incorrect-value-entered-in-url/59074/3 "2006-04-28T15:19:40Z")

</div>

Also check out the wonderful exception\_notification plugin for ideas as  
to  
how you can use this to better suit your needs.

---

<div class="post-metadata">

**Author:** ![aftershock](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8e7dd6/32.png) [@aftershock](https://www.ruby-forum.com/u/aftershock)\
**Post date:** [April 28, 2006, 3:19pm UTC](https://www.ruby-forum.com/t/handling-errors-incorrect-value-entered-in-url/59074/4 "2006-04-28T15:19:33Z")

</div>

James, take a look at this:

[http://rubyonrails.org/api/classes/ActionController/Rescue.html#M000069](http://rubyonrails.org/api/classes/ActionController/Rescue.html#M000069)

Implement the ‘rescue\_action\_in\_public’ method in your controller (or in  
application.rb) to define behavior based on a common exception. You  
could  
display a 404, or whatever you want.
