views:

129

answers:

2

Im trying make a login page for my rails application that looks like "www.domain.com" and when you login you still are still located at the domain "www.domain.com". Is there a way that I can map 2 different actions to the same url using routes. Twitter does it this way, you log in at twitter.com and after you are logged in you are still located at twitter.com.

Thanks.

A: 

After successful login redirect to the root URL.

routes.rb

map.resources :landings
# let's assume that, home page corresponds to landings/index
map.root :controller => "landings", :action => "index" 

UserSessionsController

def create
  @user_session = UserSession.new(params[:user_session])
  if @user_session.save 
    redirect_to root_url
  else
    render :action => :new
  end    
end
KandadaBoggu
+1  A: 

You can't do this by simply modifying the routes, but you can do some kind of conditional statement in your controller.

def index
  if logged_in
    render :action => 'show'
  else
    render :action => 'new'
  end
end

def show
  ...
end

def new
  ...
end

There are going to be numerous ways to do this, of course.

Karl
You are right, but `render :action => 'show'` does not run that action. It only renders a corresponding view. So everything that is needed to do in 'show' and 'new' action should be done in 'index' action (according to your naming).
klew
@kley - Good point. It may be better to use a redirect, otherwise initialize anything you would need for the action before calling render.
Karl
If you would use redirect then url would change. @espinet doesn't want it to change :)
klew