質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Devise

Deviseとは、Ruby-on-Railsの認証機能を追加するプラグインです。

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Q&A

解決済

1回答

4410閲覧

deviseを使用して、ユーザーがサインインをした後のページの移動先を変更するにはどうすれば良いのでしょうか?

ShouheyHayashi

総合スコア8

Devise

Deviseとは、Ruby-on-Railsの認証機能を追加するプラグインです。

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

0グッド

0クリップ

投稿2017/06/03 02:15

編集2017/06/04 13:00

現在、アプリの開発をしている、駆け出しプログラマーです。

deviseを使用して、一通りのログイン機能を実装させることができたのですが、after_sign_up_path_forを使用して、ユーザーがサインインをした後にroot画面に遷移するのではなく、別に用意したページに遷移させたいと思っています。

調べた上で、users controller上にregistrations_controller.rbを新たに作成し、コントローラー上で”after_sign_up_path_for”を定義し、パスを指定したのですが、何度やってもrootで指定した画面に遷移してしまいます。どうすればafter_sign_up_path_for上に指定したパスへと画面を遷移することができるのでしょう?教えていただけると、幸いです。よろしくお願いいたします。

routes.rb

ruby

1IntReviewProduct::Application.routes.draw do 2 3 devise_for :users, controllers: { 4 :registrations=>'users/registrations', 5 :passwords=>'users/passwords', 6 :sessions=>'users/sessions', 7 :unlocks=>'users/unlocks', 8 :omniauth_callbacks=>'users/omniauth_callbacks' 9 } 10 get "pages/notification" 11 resources :abouts, only: :index 12 resources :terms, only: :index 13 resources :users, only: :show 14 resources :products, only: :show do 15 collection do 16 get 'search' 17 end 18 end 19 resources :reviews, only: [:new, :create] do 20 collection do 21 get 'search' 22 end 23 end 24 root 'products#index' 25 26end

registrations_controller.rb

ruby

1class Users::RegistrationsController < Devise::RegistrationsController 2 def after_sign_up_path_for(resource) 3 pages_notification_path 4 end 5end

rake routesの表示結果

Prefix Verb URI Pattern Controller#Action new_user_session GET /users/sign_in(.:format) users/sessions#new user_session POST /users/sign_in(.:format) users/sessions#create destroy_user_session DELETE /users/sign_out(.:format) users/sessions#destroy user_password POST /users/password(.:format) users/passwords#create new_user_password GET /users/password/new(.:format) users/passwords#new edit_user_password GET /users/password/edit(.:format) users/passwords#edit PATCH /users/password(.:format) users/passwords#update PUT /users/password(.:format) users/passwords#update cancel_user_registration GET /users/cancel(.:format) users/registrations#cancel user_registration POST /users(.:format) users/registrations#create new_user_registration GET /users/sign_up(.:format) users/registrations#new edit_user_registration GET /users/edit(.:format) users/registrations#edit PATCH /users(.:format) users/registrations#update PUT /users(.:format) users/registrations#update DELETE /users(.:format) users/registrations#destroy user_confirmation POST /users/confirmation(.:format) devise/confirmations#create new_user_confirmation GET /users/confirmation/new(.:format) devise/confirmations#new GET /users/confirmation(.:format) devise/confirmations#show pages_notification GET /pages/notification(.:format) pages#notification abouts GET /abouts(.:format) abouts#index terms GET /terms(.:format) terms#index user GET /users/:id(.:format) users#show search_products GET /products/search(.:format) products#search product GET /products/:id(.:format) products#show search_reviews GET /reviews/search(.:format) reviews#search reviews POST /reviews(.:format) reviews#create new_review GET /reviews/new(.:format) reviews#new root GET / products#index

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

自己解決

自己解決しました。

after_sign_up_path_forは、userの確認を取れたのちにサインインをした後のパスであり、私はuserのconfirmationの為に、メールの設定を義務付けたので、ユーザーが新規登録画面を押しただけでは、このパスは作動しませんでした。

その代わりに、”after_inactive_sign_up_path_for”を使用することで、まだ本人確認が行われていない場合のサイトの遷移を指定することができました。

参考URL:
https://stackoverflow.com/questions/8003347/overriding-devise-after-sign-up-path-for-not-working

投稿2017/06/06 11:17

ShouheyHayashi

総合スコア8

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問