# Redirect for render\_component

**URL:** <https://www.ruby-forum.com/t/redirect-for-render-component/151132>\
**Category:** Rails\
**Created:** [October 17, 2008, 8:09am UTC](https://www.ruby-forum.com/t/redirect-for-render-component/151132 "2008-10-17T08:09:20Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![balavrscse](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/a88e4f/32.png) [@balavrscse](https://www.ruby-forum.com/u/balavrscse)\
**Post date:** [October 17, 2008, 8:09am UTC](https://www.ruby-forum.com/t/redirect-for-render-component/151132/1 "2008-10-17T08:09:20Z")

</div>

Hi,  
i am using  
\<%=render\_component(:controller=\>“mycontroller”,:action=\>“myaction”)%\>  
on the view.

in my controller i am checking the conditions whether session is there  
or not.  
if session is there i am rendering myaction view, otherwise i am  
redirecting to login controller. but it rendering the login page again  
instead of redirecting.can anybody help me.is it possible to redirect  
for render\_component?.

Thanks  
Bala

---

<div class="post-metadata">

**Author:** ![balavrscse](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/a88e4f/32.png) [@balavrscse](https://www.ruby-forum.com/u/balavrscse)\
**Post date:** [October 17, 2008, 10:02am UTC](https://www.ruby-forum.com/t/redirect-for-render-component/151132/2 "2008-10-17T10:02:21Z")

</div>

Ditch the component. Whatever you’re wanting to display with “myaction”,  
work  
it in to the view with partials or as the view itself.

A cleaner way of doing this is by using a before filter in  
“mycontroller”.  
Create an action that will check for the presence of session, and call  
it as  
a before filter.

We’ll call this action “check\_for\_session”. In your controller, at the  
top,  
put this:

before\_filter :check\_for\_session

Then, define the “check\_for\_session” action in your controller and stick  
your  
session-checking logic in there. Simply use redirect\_to on failures,  
which  
will send users without sessions to the login page.

Problem solved. When “myaction” is called, the controller will run  
check\_for\_session first. If check\_for\_session doesn’t see any session,  
it  
will redirect to the login page. Nice and clean.
