# Begin/rescue not working?

**URL:** <https://www.ruby-forum.com/t/begin-rescue-not-working/118832>\
**Category:** Rails\
**Created:** [October 31, 2007, 2:40pm UTC](https://www.ruby-forum.com/t/begin-rescue-not-working/118832 "2007-10-31T14:40:57Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![jfoxny](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a6a055/32.png) [@jfoxny](https://www.ruby-forum.com/u/jfoxny)\
**Post date:** [October 31, 2007, 2:40pm UTC](https://www.ruby-forum.com/t/begin-rescue-not-working/118832/1 "2007-10-31T14:40:57Z")

</div>

Can someone help me with the following code? I’m purposely typing in a  
bogus rga number, and the begin/rescue block should catch that and  
inform the user, right?

But when I run this with a bogus number, I get the “Render and/or  
redirect were called multiple times in this action” error.

What am I doing wrong here?

Thanks!

def assign  
@users = User.find(:all, :order =\> ‘name’)

if request.post?  
rga\_number = params[:rga\_number]  
user\_id = params[:user\_id]

```
begin
  rga = Rga.find(params[:rga_number])
rescue
  flash[:notice] = 'There is not a RGA with that number'
  redirect_to :action => 'list'
end

new_RGA = RgaEvent.new
new_RGA.rga_id = rga_number
new_RGA.rga_number = rga_number
new_RGA.user_id = user_id

if new_RGA.save
  user_name = User.find(user_id).name
  flash[:notice] = 'RGA assigned to user ' + user_name
  redirect_to :action => 'list'
end

```

end  
end

---

<div class="post-metadata">

**Author:** ![jfoxny](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a6a055/32.png) [@jfoxny](https://www.ruby-forum.com/u/jfoxny)\
**Post date:** [October 31, 2007, 2:57pm UTC](https://www.ruby-forum.com/t/begin-rescue-not-working/118832/2 "2007-10-31T14:57:03Z")

</div>

On 10/31/07, Jason F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Can someone help me with the following code? I’m purposely typing in a  
> bogus rga number, and the begin/rescue block should catch that and  
> inform the user, right?

It does; that’s not the problem.

> if request.post?  
> rga\_number = params[:rga\_number]  
> user\_id = params[:user\_id]
> 
> ```
> begin
> rga = Rga.find(params[:rga_number])
> rescue
> flash[:notice] = 'There is not a RGA with that number'
> redirect_to :action => 'list'
> 
> ```

You need to “return” right here, or execution continues on. That’s why  
you’re getting the error message.

You probably should rescue ActiveRecord::RecordNotFound. If a  
different exception were to be raised, you would never know it.

---

<div class="post-metadata">

**Author:** ![jfoxny](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a6a055/32.png) [@jfoxny](https://www.ruby-forum.com/u/jfoxny)\
**Post date:** [October 31, 2007, 3:00pm UTC](https://www.ruby-forum.com/t/begin-rescue-not-working/118832/3 "2007-10-31T15:00:53Z")

</div>

Ok,

I’ve changed the code to:

begin  
rga = Rga.find(params[:rga\_number])  
rescue ActiveRecord::RecordNotFound  
flash[:notice] = ‘There is not a RGA with that number’  
redirect\_to :action =\> ‘list’  
end

Still getting the error. You’re dealing with a newbie here: What do  
you mean by ‘return’?

---

<div class="post-metadata">

**Author:** ![jfoxny](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a6a055/32.png) [@jfoxny](https://www.ruby-forum.com/u/jfoxny)\
**Post date:** [October 31, 2007, 3:05pm UTC](https://www.ruby-forum.com/t/begin-rescue-not-working/118832/4 "2007-10-31T15:05:16Z")

</div>

On 10/31/07, Jason F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> end
> 
> Still getting the error. You’re dealing with a newbie here: What do  
> you mean by ‘return’?

I mean to add a “return” statement:

```
  redirect_to :action => 'list'
  return # <-- add this line

```

end

---

<div class="post-metadata">

**Author:** ![jfoxny](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a6a055/32.png) [@jfoxny](https://www.ruby-forum.com/u/jfoxny)\
**Post date:** [October 31, 2007, 3:22pm UTC](https://www.ruby-forum.com/t/begin-rescue-not-working/118832/5 "2007-10-31T15:22:13Z")

</div>

On 31 Oct 2007, at 14:07, Jason F. wrote:

> one, so I’m still confused as to why this case required a return in  
> it.

This has nothing to do with begin/rescue. It has everything to do with  
the fact that you should redirect or render precisely once per action,  
whereas in you original code you would end up doing a redirect in the  
rescue block and then again if bit below.

Fred

---

<div class="post-metadata">

**Author:** ![jfoxny](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a6a055/32.png) [@jfoxny](https://www.ruby-forum.com/u/jfoxny)\
**Post date:** [October 31, 2007, 3:24pm UTC](https://www.ruby-forum.com/t/begin-rescue-not-working/118832/6 "2007-10-31T15:24:20Z")

</div>

> you should redirect or render precisely once per action,

How do you possibly check for multiple errors in an action? Aren’t you  
supposed to redirect after detecting one?

---

<div class="post-metadata">

**Author:** ![jfoxny](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a6a055/32.png) [@jfoxny](https://www.ruby-forum.com/u/jfoxny)\
**Post date:** [October 31, 2007, 3:07pm UTC](https://www.ruby-forum.com/t/begin-rescue-not-working/118832/7 "2007-10-31T15:07:59Z")

</div>

> I mean to add a “return” statement:
> 
> ```
> redirect_to :action => 'list'
> return # <-- add this line
> 
> ```
> 
> end

Worked perfectly! Thanks!

I’ve used the begin/rescue/end in other controllers like I did in this  
one, so I’m still confused as to why this case required a return in it.

---

<div class="post-metadata">

**Author:** ![jfoxny](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a6a055/32.png) [@jfoxny](https://www.ruby-forum.com/u/jfoxny)\
**Post date:** [October 31, 2007, 3:51pm UTC](https://www.ruby-forum.com/t/begin-rescue-not-working/118832/8 "2007-10-31T15:51:30Z")

</div>

On 31 Oct 2007, at 14:24, Jason F. wrote:

> > you should redirect or render precisely once per action,
> 
> How do you possibly check for multiple errors in an action? Aren’t  
> you  
> supposed to redirect after detecting one?  
> OK, so at a basic level, this will result in the double render error

def foo  
redirect\_to :action =\> ‘bar’  
redirect\_to :action =\> ‘bar’  
end

so  
def foo  
if …  
redirect\_to :action =\> ‘bar’  
end

if …  
redirect\_to :action =\> ‘bar’  
end  
end

will also cause errors unless the 2 conditions are mutually exclusive

You need to do something like. Either return after redirecting/  
rendering or do whatever you have to do to ensure that you don’t do  
it more than once per action, for example

def foo  
if …  
redirect\_to :action =\> ‘bar’  
return  
end

if …  
redirect\_to :action =\> ‘bar’  
return  
end  
end

Fred

---

<div class="post-metadata">

**Author:** ![jfoxny](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a6a055/32.png) [@jfoxny](https://www.ruby-forum.com/u/jfoxny)\
**Post date:** [October 31, 2007, 3:58pm UTC](https://www.ruby-forum.com/t/begin-rescue-not-working/118832/9 "2007-10-31T15:58:03Z")

</div>

Ok, that makes sense.

Thanks guys!
