# Question about exception

**URL:** <https://www.ruby-forum.com/t/question-about-exception/149483>\
**Category:** Rails\
**Created:** [September 25, 2008, 9:54am UTC](https://www.ruby-forum.com/t/question-about-exception/149483 "2008-09-25T09:54:16Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [September 25, 2008, 9:54am UTC](https://www.ruby-forum.com/t/question-about-exception/149483/1 "2008-09-25T09:54:16Z")

</div>

Hi  
In the controller I have the function

def convert\_sd\_ticket\_to\_incident  
ServiceDeskTicket.close\_ticket(sd\_id,created\_by\_id,modified\_by\_id)  
Incident.convert\_sd\_ticket\_to\_incident(sd\_id,created\_by\_id,modified\_by\_id)  
end

Now in the ServiceDeskTicket model  
def self.close\_ticket(sd\_id,created\_by,modified\_by)  
begin  
ActiveRecord::Base.transaction do  
sd\_ticket=self.find(sd\_id)  
sd\_ticket.service\_desk\_status\_histories.add\_status\_histories\_on\_convert\_to\_incident(sd\_ticket.id,sd\_ticket.service\_desk\_status\_id,created\_by,modified\_by)  
sd\_ticket.update\_attributes!(:service\_desk\_status\_id =\> 7 )  
code continues  
end  
rescue Exception =\> e:  
puts 'error is '+e.to\_s  
end  
end

```
Now my problem is this ServiceDeskTicket has a validation

```

validates\_presence\_of :description  
It is only implemented now and there are some records in the db  
which have no description…So suppose if I tries to convert any such  
record to an incident what happens is the above code in the model  
ServiceDeskTicket rollsback since it gets exception that description  
empty…But what happens now is in the controller code above the rest ie  
Incident.convert\_sd\_ticket\_to\_incident(sd\_id,created\_by\_id,modified\_by\_id)  
which is executed (this code converts the ticket to incident)  
The solution which i am searching is how can I handle (get) the  
above exception that is happening in the model in the controller and  
redirect to the same page saying that “Please give a description first”  
I know I can restrict the Incident being added to check whether  
the return value of  
ServiceDeskTicket.close\_ticket(sd\_id,created\_by\_id,modified\_by\_id) is  
true  
But how can I handle this.Please help

Sijo

---

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [September 25, 2008, 10:16am UTC](https://www.ruby-forum.com/t/question-about-exception/149483/2 "2008-09-25T10:16:05Z")

</div>

If you need to notify the user of error, the best way is using flash  
(in the controller). For instance, in the rescue part of that last  
function do something like this:

rescue Exception =\> e  
flash[:error] = e.to\_s  
redirect :controller =\> … , :action =\> …  
end

And in the view of the page you are redirecting to, make sure you have  
a reserved space to show flash messages like so:

\<%= flash[:error] -%\>

Hope it was of help.
