# Redirecting in a view?

**URL:** <https://www.ruby-forum.com/t/redirecting-in-a-view/52970>\
**Category:** Rails\
**Created:** [January 21, 2006, 10:05pm UTC](https://www.ruby-forum.com/t/redirecting-in-a-view/52970 "2006-01-21T22:05:43Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![csn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/db5fbb/32.png) [@csn](https://www.ruby-forum.com/u/csn)\
**Post date:** [January 21, 2006, 10:05pm UTC](https://www.ruby-forum.com/t/redirecting-in-a-view/52970/1 "2006-01-21T22:05:43Z")

</div>

Is there a way to do this in a view?

\<% redirect\_to ‘[http://url.com](http://url.com)’ %\>

All I can think of is outputting a link and using  
Javascript to click it, or probably do something like  
‘doc.location(url)’.

csn

* * *

Do You Yahoo!?  
Tired of spam? Yahoo! Mail has the best spam protection around

> **[Yahoo Mail - Organized Email](https://mail.yahoo.com)**
>
> It's time to get stuff done with Yahoo Mail. Just add your Gmail, Outlook, AOL or Yahoo Mail to get going. We automatically organize all the things life throws at you, like receipts and attachments, so you can find what you need fast. Plus, we've got...

---

<div class="post-metadata">

**Author:** ![csn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/db5fbb/32.png) [@csn](https://www.ruby-forum.com/u/csn)\
**Post date:** [January 21, 2006, 10:34pm UTC](https://www.ruby-forum.com/t/redirecting-in-a-view/52970/2 "2006-01-21T22:34:19Z")

</div>

You could use a meta refresh if it is supposed to be after a set time  
limit.

---

<div class="post-metadata">

**Author:** ![csn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/db5fbb/32.png) [@csn](https://www.ruby-forum.com/u/csn)\
**Post date:** [January 21, 2006, 10:37pm UTC](https://www.ruby-forum.com/t/redirecting-in-a-view/52970/3 "2006-01-21T22:37:26Z")

</div>

Ben R. wrote:

> You could use a meta refresh if it is supposed to be after a set time  
> limit.

Would that work in BODY though? Where I want to redirect is in a  
template that goes in @content\_for\_layout in between body tags.

csn

---

<div class="post-metadata">

**Author:** ![csn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/db5fbb/32.png) [@csn](https://www.ruby-forum.com/u/csn)\
**Post date:** [January 21, 2006, 10:39pm UTC](https://www.ruby-forum.com/t/redirecting-in-a-view/52970/4 "2006-01-21T22:39:58Z")

</div>

You can send back some javascript, off the top of my head it would be:

\<%= javascript\_tag  
‘document.location=“[http://www.domain.com/link.html](http://www.domain.com/link.html)”;’ %\>

Cheers, Jonny.

csn wrote:

> Ben R. wrote:
> 
> > You could use a meta refresh if it is supposed to be after a set time  
> > limit.
> 
> Would that work in BODY though? Where I want to redirect is in a  
> template that goes in @content\_for\_layout in between body tags.
> 
> csn

---

<div class="post-metadata">

**Author:** ![csn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/db5fbb/32.png) [@csn](https://www.ruby-forum.com/u/csn)\
**Post date:** [January 21, 2006, 10:53pm UTC](https://www.ruby-forum.com/t/redirecting-in-a-view/52970/5 "2006-01-21T22:53:07Z")

</div>

Why does your controller not have enough information to make this  
decision,  
but your view does?

This type of work should be kept out of the view code and kept in the  
controller. That’s what MVC is about, and will help you in the future  
when  
your app needs to be maintained.

---

<div class="post-metadata">

**Author:** ![csn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/db5fbb/32.png) [@csn](https://www.ruby-forum.com/u/csn)\
**Post date:** [January 22, 2006, 4:19am UTC](https://www.ruby-forum.com/t/redirecting-in-a-view/52970/6 "2006-01-22T04:19:11Z")

</div>

Sounds like its trying to control something. This is a good job for the  
controller.

---

<div class="post-metadata">

**Author:** ![csn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/db5fbb/32.png) [@csn](https://www.ruby-forum.com/u/csn)\
**Post date:** [January 22, 2006, 3:39am UTC](https://www.ruby-forum.com/t/redirecting-in-a-view/52970/7 "2006-01-22T03:39:03Z")

</div>

Tom F. wrote:

> Why does your controller not have enough information to make this  
> decision,  
> but your view does?
> 
> This type of work should be kept out of the view code and kept in the  
> controller. That’s what MVC is about, and will help you in the future  
> when  
> your app needs to be maintained.

I agree ;). But it’s just a generic sub template that does one of the  
following:

- links to a file (image, flash)
- includes another file (text)
- renders a template in a database field
- or (hopefully) redirects to another site

csn
