LoginSignup
1
1

More than 3 years have passed since last update.

【Rails】ユーザー情報変更後投稿一覧ページに遷移する

Posted at

現在作成中のポートフォリオでは、ユーザー機能にdeviseを利用しています。

今回はユーザー情報変更後、指定のページに戻る方法を学びましたのメモしておきます。

registrations_controllerの設定

registrations_controller.rbで以下の部分のコメントアウトを外してください。

registrations_controller.rb
def after_update_up_path_for(resource)
    # ここにページ遷移したいパスをいれる
    home_show_path(resource)
  end

私の場合はhome/showページに飛ばしたかったのでhome_show_pathと記入しています。

routesの設定

次はルートです。
以下のように記述してください。

routes.rb
as :user do
  get 'home/show',:to =>'devise/registrations#edit',:as => :user_root
end

asメソッドを使うと、どのリソースがルートかを伝えてくれるため、指定ページに簡単にリダイレクトしてくれます。

まとめ

今回のパターンを含め、他パターンは公式で紹介されているのでそちらもぜひ参考にしてください。
How To: Customize the redirect after a user edits their profile

deviseは便利ですが結構複雑な印象があるので、色々試してなれていきたいです。

この記事が誰かの助けになれば幸いです。
ではでは。

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