Forum: Ruby on Rails How can I forward to different pages for different account

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.
Zhao Y. (Guest)
on 2009-03-25 03:23
My web site has a login feature. What I want to do is to design
different page for different account. But in an action, I couldn't use
redirect_to more than one time. I am looking for a way like this:

if account == "admin"
    redirect_to ...
else
    redirect_to ...
...
(Guest)
on 2009-03-25 08:57
(Received via mailing list)
On 25 Mar., 02:23, Zhao Yi <removed_email_address@domain.invalid> wrote:
> My web site has a login feature. What I want to do is to design
> different page for different account. But in an action, I couldn't use
> redirect_to more than one time. I am looking for a way like this:
>
> if account == "admin"
>     redirect_to ...
> else
>     redirect_to ...
> ...

That is because Rails only allows to do a single redirect or render
per action. You can get around this by adding "and return" after the
first redirect to prevent the rest of the code to get evaluated.

if account == "admin"
  redirect_to some_page_path and return
else
  redirect_to some_other_page_path
end

But do you really get an error when trying to do more than one
redirect, when it's inside an if/else statement? The "else" part
should really not get evaluated if account == "admin"...

--
Best regards,
David K.
http://twitter.com/rubyguy
Greg D. (Guest)
on 2009-03-25 17:33
(Received via mailing list)
On Tue, Mar 24, 2009 at 8:23 PM, Zhao Yi
<removed_email_address@domain.invalid> wrote:
> My web site has a login feature. What I want to do is to design
> different page for different account. But in an action, I couldn't use
> redirect_to more than one time. I am looking for a way like this:
>
> if account == "admin"
>    redirect_to ...

Add "return false" here.

> else
>    redirect_to ...

and here too.

You have to make sure you only redirect once, returning false after
the initial redirect will ensure this.



--
Greg D.
http://destiney.com/
pepe (Guest)
on 2009-03-28 05:32
(Received via mailing list)
Hello.

The problem with redirect_to is that it is counter-intuitive. It seems
to say, when the code gets here I am going somewhere and never return.
That is not the case. It is just like any other method call, you call
it and it returns and then your code after the redirect_to keeps
getting executed. That is the reason you should use return, as the
rest of the posts suggest.

Good luck.

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