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.
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'
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.
I mean to add a "return" statement:
redirect_to :action => 'list'
return # <-- add this line
end
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
OK, so at a basic level, this will result in the double render error
def foo
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