开发者

2 pages using the same url using rails routes

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开发者_StackOverflow社区 it this way, you log in at twitter.com and after you are logged in you are still located at twitter.com.


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.


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜