2 pages using the same URL using rail routes

I am trying to make a login page for my rails application that looks like "www.domain.com" and when you log in you are still in 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 to twitter.com, and after you logged in, you are still on twitter.com.

+3
source share
2 answers

You cannot do this by simply changing the routes, but you can make 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

, .

+1

URL.

routes.rb

map.resources :landings
# let 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

Source: https://habr.com/ru/post/1737045/


All Articles