LoginSignup
64
62

More than 5 years have passed since last update.

[Rails]ログインしてる時としてない時で異なるルーティングを指定する

Last updated at Posted at 2014-07-10

http://facebook.com のように同じURLだけどログイン前はランディングページ、ログインしてたらタイムラインを表示するようにしたい。機能が全く違うので同じcontrollerで処理を切り分けるとかはしたくない。

この場合routingでsession情報を判定して切り分けるといい感じ。

config/routes.rb
  constraints ->  request { request.session[:user_id].present? } do
    # ログインしてる時のパス
    root to: "dashboard#index"
  end
  # ログインしてない時のパス
  root to: 'welcome#index'
64
62
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
64
62