# Noob: Error object in controller?

**URL:** <https://www.ruby-forum.com/t/noob-error-object-in-controller/89850>\
**Category:** Rails\
**Created:** [March 23, 2007, 10:50am UTC](https://www.ruby-forum.com/t/noob-error-object-in-controller/89850 "2007-03-23T10:50:09Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Thogra](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ea666f/32.png) [@Thogra](https://www.ruby-forum.com/u/Thogra)\
**Post date:** [March 23, 2007, 10:50am UTC](https://www.ruby-forum.com/t/noob-error-object-in-controller/89850/1 "2007-03-23T10:50:09Z")

</div>

Hi all.

First some confession I have to make: I’m coming from Java. This will  
possibly explain my question 😉

In Java you have an error object when -hm- some error has happened.  
You can ask this object for the error message, which is what I  
expected to have in a Rails action as well. For example when doing  
this:  
4 / 0  
in my controller I can see a division by zero error message in the  
view. How do I get access to this error message & object in my  
controller ?

Thanks

---

<div class="post-metadata">

**Author:** ![Thogra](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ea666f/32.png) [@Thogra](https://www.ruby-forum.com/u/Thogra)\
**Post date:** [March 23, 2007, 12:18pm UTC](https://www.ruby-forum.com/t/noob-error-object-in-controller/89850/2 "2007-03-23T12:18:22Z")

</div>

> In Java you have an error object when -hm- some error has happened.  
> You can ask this object for the error message,  
> i assume you are talking about exceptions. And in ruby there are  
> exceptions too. If you are going to program i’d advise you to take a  
> look at a ruby manual.

anyway, a quick comparison between java/ruby exception handling

java:  
try{  
}catch(Exception e){  
System.out.println(e.getMessage())  
} finally { }

ruby  
begin  
rescue Exception=\>e  
puts e.message  
ensure  
end

there are a couple of nice add-ons with exceptions in ruby. You can use  
“retry” in the rescue block and you can use “else” in case no exceptions  
are catched. Also you can inline the rescue for a single line making  
your code more compact and easier to follow.

just be careful about which exceptions you are rescueing, because the  
type Exception will not cover them all.

and one more bonus… you have in the special variable $! the last  
exception raised. (By the way, raise is the ruby equivalent to java  
throw)

regards,

javier ramirez

## –

Estamos de estreno… si necesitas llevar el control de tus gastos  
visita [http://www.gastosgem.com](http://www.gastosgem.com) !!Es gratis!!

---

<div class="post-metadata">

**Author:** ![Thogra](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ea666f/32.png) [@Thogra](https://www.ruby-forum.com/u/Thogra)\
**Post date:** [March 23, 2007, 1:21pm UTC](https://www.ruby-forum.com/t/noob-error-object-in-controller/89850/3 "2007-03-23T13:21:34Z")

</div>

Thanks for your answer javier. I still have one question.

When something goes wrong while saving a model in a controller I don’t  
want the red error box the scaffolding generates. I’d prefer to  
display that error message in a customized way. I already tried to  
read the errors object in my controller. But it doesn’t seem to exist  
in the controller.

---

<div class="post-metadata">

**Author:** ![Thogra](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ea666f/32.png) [@Thogra](https://www.ruby-forum.com/u/Thogra)\
**Post date:** [March 23, 2007, 9:18pm UTC](https://www.ruby-forum.com/t/noob-error-object-in-controller/89850/4 "2007-03-23T21:18:14Z")

</div>

Hi,

> When something goes wrong while saving a model in a controller I don’t  
> want the red error box the scaffolding generates. I’d prefer to  
> display that error message in a customized way. I already tried to  
> read the errors object in my controller. But it doesn’t seem to exist  
> in the controller.

well… if you don’t like the layout or the colors of the message, then  
just take a look at the css it’s using (i’m afraid i cannot help you  
here, i’m not a fan of scaffold myself, so no idea how it is).

but… if what you are after is the messages’ content, then take a look  
at [Peak Obsession](http://api.rubyonrails.com/classes/ActiveRecord/Errors.html#M000966)

good luck,

javier ramirez

## –

Estamos de estreno… si necesitas llevar el control de tus gastos  
visita [http://www.gastosgem.com](http://www.gastosgem.com) !!Es gratis!!
