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

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

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

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

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Q&A

解決済

1回答

3112閲覧

devise で新規登録後の遷移先がrootになってしまう

kwtkwt

総合スコア21

Ruby

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

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

0グッド

0クリップ

投稿2021/09/23 06:09

前提・実現したいこと

devise で新規登録ごの遷移先を変更したいです。

二つのログイン機能をdeviseで実装しています。

片方(user_devises)はうまく指定した通り、遷移してくれるのですが、もう片方(companies)で同じように、指定してもうまくいかず、rootに遷移してしまします。

両方とも指定した通り、遷移するようにしたいです。

該当のソースコード

app/controllers/companies/registrations_controller.rb

ruby

1# frozen_string_literal: true 2 3class Companies::RegistrationsController < Devise::RegistrationsController 4 # before_action :configure_sign_up_params, only: [:create] 5 # before_action :configure_account_update_params, only: [:update] 6 7 # GET /resource/sign_up 8 # def new 9 # super 10 # end 11 12 # POST /resource 13 # def create 14 # super 15 # end 16 17 # GET /resource/edit 18 # def edit 19 # super 20 # end 21 22 # PUT /resource 23 # def update 24 # super 25 # end 26 27 # DELETE /resource 28 # def destroy 29 # super 30 # end 31 32 # GET /resource/cancel 33 # Forces the session data which is usually expired after sign 34 # in to be expired now. This is useful if the user wants to 35 # cancel oauth signing in/up in the middle of the process, 36 # removing all OAuth session data. 37 # def cancel 38 # super 39 # end 40 41 # protected 42 43 # If you have extra params to permit, append them to the sanitizer. 44 # def configure_sign_up_params 45 # devise_parameter_sanitizer.permit(:sign_up, keys: [:attribute]) 46 # end 47 48 # If you have extra params to permit, append them to the sanitizer. 49 # def configure_account_update_params 50 # devise_parameter_sanitizer.permit(:account_update, keys: [:attribute]) 51 # end 52 53 #アカウント登録後のリダイレクト先 54 def after_sign_up_path_for(resource) 55 new_company_registration_path 56 end 57 58 # The path used after sign up for inactive accounts. 59 def after_inactive_sign_up_path_for(resource) 60 new_company_registration_path 61 end 62end

app/controllers/user_devises/registrations_controller.rb

ruby

1# frozen_string_literal: true 2 3class UserDevises::RegistrationsController < Devise::RegistrationsController 4 # before_action :configure_sign_up_params, only: [:create] 5 # before_action :configure_account_update_params, only: [:update] 6 7 # GET /resource/sign_up 8 # def new 9 # super 10 # end 11 12 # POST /resource 13 # def create 14 # super 15 # end 16 17 # GET /resource/edit 18 # def edit 19 # super 20 # end 21 22 # PUT /resource 23 # def update 24 # super 25 # end 26 27 # DELETE /resource 28 # def destroy 29 # super 30 # end 31 32 # GET /resource/cancel 33 # Forces the session data which is usually expired after sign 34 # in to be expired now. This is useful if the user wants to 35 # cancel oauth signing in/up in the middle of the process, 36 # removing all OAuth session data. 37 # def cancel 38 # super 39 # end 40 41 # protected 42 43 # If you have extra params to permit, append them to the sanitizer. 44 # def configure_sign_up_params 45 # devise_parameter_sanitizer.permit(:sign_up, keys: [:attribute]) 46 # end 47 48 # If you have extra params to permit, append them to the sanitizer. 49 # def configure_account_update_params 50 # devise_parameter_sanitizer.permit(:account_update, keys: [:attribute]) 51 # end 52 53 #アカウント登録後のリダイレクト先 54 def after_sign_up_path_for(resource) 55 new_user_devise_session_path 56 end 57 58 #アカウント情報、プロフィール編集後のリダイレクト先 59 def after_update_path_for(resource) 60 # マイページへ遷移 61 user_devise_path(resource.id) 62 end 63 64 # The path used after sign up for inactive accounts. 65 def after_inactive_sign_up_path_for(resource) 66 new_user_devise_session_path 67 end 68end 69

app/views/devise/registrations/new.html.erb

erb

1<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %> 2 <%= render "devise/shared/error_messages", resource: resource %> 3 4 <div class="p-register__field"> 5 <%= f.label :メールアドレス %><br /> 6 <%= f.email_field :email, autofocus: true, autocomplete: "email" %> 7 </div> 8 9 <div class="p-register__field"> 10 <%= f.label :パスワード %> 11 <% if @minimum_password_length %> 12 <em>(<%= @minimum_password_length %>文字以上)</em> 13 <% end %><br /> 14 <%= f.password_field :password, autocomplete: "new-password" %> 15 </div> 16 17 <div class="p-register__field"> 18 <%= f.label :パスワード確認 %><br /> 19 <%= f.password_field :password_confirmation, autocomplete: "new-password" %> 20 </div> 21 22 23 <div class="p-register__actions"> 24 <p><a href="">利用規約</a>に同意して</p> 25 <%= f.submit "登録する", class: "c-btn__register u-mtb20" %> 26 <p>すでに登録されている方は<a href="/user_devises/sign_in">こちら</a></p> 27 28 </div> 29 <% end %>

補足情報(FW/ツールのバージョンなど)

Rails 5.1.6

よろしくお願いいたします。

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

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

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

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

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

guest

回答1

0

自己解決

ルーティングを書き換えたら治りました。

ruby

1devise_for :companies, controllers: { registrations: 'companies/registrations'}

投稿2021/09/23 11:15

kwtkwt

総合スコア21

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問