Noob: Error object in controller ?

Hi all.

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

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

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

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.

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

good luck,

javier ramirez