Forum: Ruby on Rails Developing code that can go multiple places

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Joe S. (Guest)
on 2008-11-29 23:51
In my controller code, depending on the values in the database, I may
want my user to go one of several places.  But when I put more than one
redirect in, it gives me the message:

Render and/or redirect were called multiple times in this action.

How do I do this?
Steve R. (Guest)
on 2008-11-29 23:59
(Received via mailing list)
On Nov 29, 2008, at 1:51 PM, Joe S. wrote:

>
> In my controller code, depending on the values in the database, I may
> want my user to go one of several places.  But when I put more than
> one
> redirect in, it gives me the message:
>
> Render and/or redirect were called multiple times in this action.
>
> How do I do this?
> --

redirect_to(some_place) and return

Alternatively, store the desired destination URL and redirect once at
the end of the method.

stored_location = nil
# ...
stored_location = 'fall_down_go_boom_url' if some_criteria
# ...
redirect_to stored_location unless stored_location.nil?
pepe (Guest)
on 2008-12-01 04:35
(Received via mailing list)
Hi,

Maybe s.ross's explanation is enough, but just in case, I had to deal
with the same problem a while back and this might be useful...

redirect_to doesn't stop the rest of the code in your method/action
from keeping executing so if you use it and then later on in your code
there is another render/redirect_to like, for example, as a default
action after an if/select then there is your problem.

The solution is to use return right after you redirect_to line:

redirect_to
return # here the code leaves the method/action

I hope this helps.

Pepe
This topic is locked and can not be replied to.