Redirecting From default page

Hi. I am new to Rails. I am doing a web project for my school. It has
models like Page, Student and Forum. Page model is for creating pages
like home page, help, contact, about, feedback, etc. My route has my
home page as :controller => “pages”, :action => “show”, :id => “1”.
Students have registrations to access and use the forum. I am using a
cookie based session storage. How can i redirect the student to the
forums index which is :controller => “forums”, :action => “index”
instead of home page even when he types the home page
address(www.homepage.com), if he had already logged in and had set
Remember my password.

Thanks in advance.

Sounds like you need a show-action-specific before_filter for your Pages
controller:

before_filter :redirect_to_forums, :only => [:show]

def redirect_to_forums
if logged_in? # you will need to define this
redirect_to :controller => “forums” # index action is implied
end
end

Anna Lissa

On Fri, Jul 4, 2008 at 9:34 AM, Rock R.
[email protected]

AnnaLissa C. wrote:

Sounds like you need a show-action-specific before_filter for your Pages
controller:

before_filter :redirect_to_forums, :only => [:show]

def redirect_to_forums
if logged_in? # you will need to define this
redirect_to :controller => “forums” # index action is implied
end
end

Anna Lissa

On Fri, Jul 4, 2008 at 9:34 AM, Rock R.
[email protected]

Thank you for your reply.
But i have a help page of route :controller=> “pages”, :action =>
“show”, :id => “2”, a contact page with route :controller=> “pages”,
:action => “show”, :id => “3” and so on. What will happen to the show
action for those links if the user has logged in

I see. It’s not very pretty but you can do this instead (with no
before_filter):

def show
if params[:id] == 1 && logged_in?
redirect_to :controller => “forums”
return
end

rest of your action definition

end

On Fri, Jul 4, 2008 at 11:35 AM, Rock R.
[email protected]

AnnaLissa C. wrote:

I see. It’s not very pretty but you can do this instead (with no
before_filter):

def show
if params[:id] == 1 && logged_in?
redirect_to :controller => “forums”
return
end

rest of your action definition

end

On Fri, Jul 4, 2008 at 11:35 AM, Rock R.
[email protected]

Oh. I got it. Thanks Anna for your help.

In that case, you probably would be better off having your homepage
route
being

map.root :controller => “pages”

then in your PagesController:

def index
if params[:id] == 1 && logged_in?

redirect_to :controller => "forums"
return

end
@page = Page.find(1)
render :action => :show
end

On Fri, Jul 4, 2008 at 9:28 PM, Rock R.
[email protected]

Rock R. wrote:

AnnaLissa C. wrote:

I see. It’s not very pretty but you can do this instead (with no
before_filter):

def show
if params[:id] == 1 && logged_in?
redirect_to :controller => “forums”
return
end

rest of your action definition

end

On Fri, Jul 4, 2008 at 11:35 AM, Rock R.
[email protected]

Oh. I got it. Thanks Anna for your help.

I have another doubt. I have a navigation bar available to all
controllers of the site with links Home, Help ,Contact,etc. So if i
click Home, then won’t I be redirected to forums?

oops, fat-fingered that. :slight_smile: I meant:

def index
if logged_in?
redirect_to :controller => “forums”
return
end
@page = Page.find(1)
render :action => :show
end

hope that helps

a.l.

AnnaLissa C. wrote:

oops, fat-fingered that. :slight_smile: I meant:

def index
if logged_in?
redirect_to :controller => “forums”
return
end
@page = Page.find(1)
render :action => :show
end

hope that helps

a.l.

Thank you