0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

ログイン後のルートページを変える (deviseを使用)

Posted at

Why?

ログイン前とログイン後でルートページを変えたいな~と思って調べてみました。
さっくりとした備忘録です。

やり方

deviseの公式の方で

ユーザのサインイン、アカウントの確認、パスワードの更新の後、Deviseはリダイレクト先のスコープ付きルートパスを探します。 例えば、 :userリソースを使用する場合、user_root_pathが存在すればそれが使用され、そうでなければデフォルトのroot_pathが使用されます。

と書かれていたので、rootを指定したらうまく動いてくれた。
コードはChatGPTに助けてもらったので爪甘なところあったらコメント頂けると幸いです。

ログイン前は"static_pages#start"をルートとして
ログイン後は"cards#index"をルートにする場合。

config/routes.rb
Rails.application.routes.draw do
  #ログイン後のルートページ
  authenticated :user do
    root to: "cards#index", as: :user_root
  end
  
  # ログイン前のルートページ
  unauthenticated do
    root to: "static_pages#start"
  end
  
  devise_for :users
  resources :cards, only: %i[index]
  mount LetterOpenerWeb::Engine, at: "/letter_opener" if Rails.env.development?
end

この時にログイン後のルートページをログイン前のルートページよりも前に書いておかないとログイン前の方のルートが適応されてしまうらしいので注意。

おしまい。

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?