Forum: Ruby on Rails redirect for render_component

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.
F1830398bcacccb5f629c4aec0a5962c?d=identicon&s=25 Bala Krishnan (balavrscse)
on 2008-10-17 08:09
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
84b661cb6bd8a870c1787968ee945962?d=identicon&s=25 Patrick Sullivan (ywp)
on 2008-10-17 10:02
(Received via mailing list)
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.
This topic is locked and can not be replied to.