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

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

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

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

Ruby on Rails

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

Q&A

1回答

1062閲覧

deviseで複数モデルを扱うときの実装について

takuyahori

総合スコア0

Devise

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

Ruby on Rails

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

0グッド

0クリップ

投稿2021/10/31 11:44

編集2021/11/01 12:11

deviseで複数モデルを管理する場合の実装。ユーザー新規登録をした後にトップページに遷移させたい。

Railsでdeviseを用いて実装をしています。
ユーザーを2パターンに分けたため複数モデルで管理をする実装をしており、ユーザーの新規登録完了後トップページに遷移させたいのですが、以下のエラーが出ます。
テーブルにも情報が保存されません。

Routing Error No route matches [POST] "/trainers/sign_up" Rails.root: /Users/horitakuya/projects/dm-space Application Trace | Framework Trace | Full Trace Routes Routes match in priority from top to bottom

該当のソースコード

Rails

1Rails.application.routes.draw do 2 devise_for :cliants, controllers: { 3 sessions: 'cliants/sessions', 4 passwords: 'cliants/passwords', 5 registrations: 'cliants/registrations' 6 } 7 devise_for :trainers, controllers: { 8 sessions: 'trainers/sessions', 9 passwords: 'trainers/passwords', 10 registrations: 'trainers/registrations' 11 } 12 root 'tops#index' 13end

ragistrasitons_controller.rb

1# frozen_string_literal: true 2 3class Trainers::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 @trainer = Trainer.new(configure_sign_up_params) 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 expi red 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: [:nickname, :first_name, :last_name, :profile]) 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 # The path used after sign up. 54 def after_sign_up_path_for(resource) 55 root_path(resource) 56 end 57 58 # The path used after sign up for inactive accounts. 59 # def after_inactive_sign_up_path_for(resource) 60 # super(resource) 61 # end 62end

html.erb

1<%= form_with model: @trainer, url: new_trainer_registration_path, class: 'registration-main', local: true do |f| %> 2<div class='form-wrap'> 3 <div class='gorm-header'> 4 <h1 class='form-header-text'> 5 会員情報入力 6 </h1> 7 </div> 8 <%= render 'shared/error_messages', momdel: f.object %> 9 <div class='form-group'> 10 <div class='form-text-wrap'> 11 <label class="form-text">ニックネーム</label> 12 <span class="indispensable">必須</span> 13 </div> 14 <%= f.text_area :nickname, class:"input-default", id:"nickname", placeholder:"例) 田中太郎", maxlength:"40" %> 15 </div> 16 <div class='form-group'> 17 <div class='form-text-wrap'> 18 <label class='form-text'>メールアドレス</label> 19 <span class='indispensable'>必須</span> 20 </div> 21 <%= f.email_field :email, class:"input-default", id:"email", placeholder:"PC・携帯どちらでも可", autofocus: true %> 22 </div> 23 <div class="form-group"> 24 <div class="form-text-wrap"> 25 <label class="form-text">パスワード</label> 26 <span class="indispensable">必須</span> 27 </div> 28 <%= f.password_field :password, class:"input-default", id:"password", placeholder:"6文字以上の半角英数字" %> 29 <p class='info-text'>英字と数字の両方を含めて設定してください</p> 30 </div> 31 <div class="form-group"> 32 <div class="form-text-wrap"> 33 <label class="form-text">パスワード(確認)</label> 34 <span class="indispensable">必須</span> 35 </div> 36 <%= f.password_field :password_confirmation, class:"input-default", id:"password-confirmation", placeholder:"同じパスワードを入力してください" %> 37 </div> 38 <div class="form-group"> 39 <div class="form-text-wrap"> 40 <label class="form-text">お名前(全角)</label> 41 <span class="indispensable">必須</span> 42 </div> 43 <div class="input-name-wrap"> 44 <%= f.text_area :last_name, class:"input-name", id:"last-name", placeholder:"例) 田中" %> 45 <%= f.text_area :first_name, class:"input-name", id:"first-name", placeholder:"例) 太郎" %> 46 </div> 47 </div> 48 <div class="form-group"> 49 <div class="form-text-wrap"> 50 <label class="form-text">プロフィール</label> 51 <span class="indispensable">必須</span> 52 </div> 53 <%= f.text_field :profile, class:"input-default", id:"profile" %> 54 </div> 55 <div class="register-btn"> 56 <%= f.submit "新規登録" ,class:"registar-red-btn" %> 57 </div> 58<% end %> 59</div>

試したこと

ルーティングの記述に誤りがないかの確認や、devise複数モデル管理におけるコントローラーに記述しなければならないことを調べたりしましたが、解決できずお力をお借りできますと幸いです。

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

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

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

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

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

guest

回答1

0

def after_sign_up_path_for(resource)
を定義してみてください

ちらっと見なので他にもある、かもですが
<%= form_with model: @trainer, url: new_trainer_registration_path,
ここ。
createを呼ぶので trainer_registrations_path ではないかな。
rails routes で確認を

投稿2021/10/31 12:23

編集2021/11/01 12:18
winterboum

総合スコア23347

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

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

takuyahori

2021/11/01 08:11

ご回答ありがとうございます! ご指摘いただきましたとおり、下記の通り記述してみましたがうまくいかず同じエラーが出てしまいます、、、 def after_sign_up_path_for(resource) root_path(resource) end サーバーの再起動なども試しましたがうまくいかず、またアドバイスいただけますと幸いです、、、
winterboum

2021/11/01 08:33

登録に失敗しているんでは?
takuyahori

2021/11/01 08:53

データベースにも保存できていないので登録に失敗していると思われます。 コントローラーの編集を下記の通りしておりまして、createメソッドも定義しているのですが反映されていないようでして、、、 ``` # frozen_string_literal: true class Trainers::RegistrationsController < Devise::RegistrationsController before_action :configure_sign_up_params, only: [:create] # before_action :configure_account_update_params, only: [:update] # GET /resource/sign_up def new super end # POST /resource def create @trainer = Trainer.new(configure_sign_up_params) end # GET /resource/edit # def edit # super # end # PUT /resource # def update # super # end # DELETE /resource # def destroy # super # end # GET /resource/cancel # Forces the session data which is usually expired after sign # in to be expi red now. This is useful if the user wants to # cancel oauth signing in/up in the middle of the process, # removing all OAuth session data. # def cancel # super # end # protected # If you have extra params to permit, append them to the sanitizer. def configure_sign_up_params devise_parameter_sanitizer.permit(:sign_up, keys: [:nickname, :first_name, :last_name, :profile]) end # If you have extra params to permit, append them to the sanitizer. # def configure_account_update_params # devise_parameter_sanitizer.permit(:account_update, keys: [:attribute]) # end # The path used after sign up. def after_sign_up_path_for(resource) root_path(resource) end # The path used after sign up for inactive accounts. # def after_inactive_sign_up_path_for(resource) # super(resource) # end end ``` こちらの記述に何か誤った点などございますでしょうか、、、?
winterboum

2021/11/01 09:14

ああ、「テーブルにも情報が保存されません。」とありますね。 登録関係のControllerやviewを修正かけてますか?
takuyahori

2021/11/01 09:57

コントローラーもビューもrails g deviseコマンドで作成したファイルは修正かけております。 application.controllerなどにはまだ記述していないです。
winterboum

2021/11/01 11:30

となると、その修正に問題があると思われるので、それを載せてください
takuyahori

2021/11/01 11:51 編集

度々ありがとうございます、、、 下記がビューファイルになります。 ```html.erb <%= form_with model: @trainer, url: new_trainer_registration_path, class: 'registration-main', local: true do |f| %> <div class='form-wrap'> <div class='gorm-header'> <h1 class='form-header-text'> 会員情報入力 </h1> </div> <%= render 'shared/error_messages', momdel: f.object %> <div class='form-group'> <div class='form-text-wrap'> <label class="form-text">ニックネーム</label> <span class="indispensable">必須</span> </div> <%= f.text_area :nickname, class:"input-default", id:"nickname", placeholder:"例) 田中太郎", maxlength:"40" %> </div> <div class='form-group'> <div class='form-text-wrap'> <label class='form-text'>メールアドレス</label> <span class='indispensable'>必須</span> </div> <%= f.email_field :email, class:"input-default", id:"email", placeholder:"PC・携帯どちらでも可", autofocus: true %> </div> <div class="form-group"> <div class="form-text-wrap"> <label class="form-text">パスワード</label> <span class="indispensable">必須</span> </div> <%= f.password_field :password, class:"input-default", id:"password", placeholder:"6文字以上の半角英数字" %> <p class='info-text'>英字と数字の両方を含めて設定してください</p> </div> <div class="form-group"> <div class="form-text-wrap"> <label class="form-text">パスワード(確認)</label> <span class="indispensable">必須</span> </div> <%= f.password_field :password_confirmation, class:"input-default", id:"password-confirmation", placeholder:"同じパスワードを入力してください" %> </div> <div class="form-group"> <div class="form-text-wrap"> <label class="form-text">お名前(全角)</label> <span class="indispensable">必須</span> </div> <div class="input-name-wrap"> <%= f.text_area :last_name, class:"input-name", id:"last-name", placeholder:"例) 田中" %> <%= f.text_area :first_name, class:"input-name", id:"first-name", placeholder:"例) 太郎" %> </div> </div> <div class="form-group"> <div class="form-text-wrap"> <label class="form-text">プロフィール</label> <span class="indispensable">必須</span> </div> <%= f.text_field :profile, class:"input-default", id:"profile" %> </div> <div class="register-btn"> <%= f.submit "新規登録" ,class:"registar-red-btn" %> </div> <% end %> </div> ``` 下記がコントローラーになります。 ```rails # frozen_string_literal: true class Trainers::RegistrationsController < Devise::RegistrationsController before_action :configure_sign_up_params, only: [:create] # before_action :configure_account_update_params, only: [:update] # GET /resource/sign_up def new super end # POST /resource def create @trainer = Trainer.new(configure_sign_up_params) end # GET /resource/edit # def edit # super # end # PUT /resource # def update # super # end # DELETE /resource # def destroy # super # end # GET /resource/cancel # Forces the session data which is usually expired after sign # in to be expi red now. This is useful if the user wants to # cancel oauth signing in/up in the middle of the process, # removing all OAuth session data. # def cancel # super # end # protected # If you have extra params to permit, append them to the sanitizer. def configure_sign_up_params devise_parameter_sanitizer.permit(:sign_up, keys: [:nickname, :first_name, :last_name, :profile]) end # If you have extra params to permit, append them to the sanitizer. # def configure_account_update_params # devise_parameter_sanitizer.permit(:account_update, keys: [:attribute]) # end # The path used after sign up. def after_sign_up_path_for(resource) root_path(resource) end # The path used after sign up for inactive accounts. # def after_inactive_sign_up_path_for(resource) # super(resource) # end end ```
winterboum

2021/11/01 11:53

質問を編集してそちらに。 ここだとベタなので読みにくい
takuyahori

2021/11/01 12:12

編集いたしました! 該当のソースコードの下二つがコントローラーとビューになります。
takuyahori

2021/11/01 12:47

pathはご指摘いただいた通りでした! 修正して再度新規登録を実行したところ、エラー画面は表示されなくなったのですが、 新規登録ボタンを押してもリロードされず、画面が変わらないということが起きました、、
takuyahori

2021/11/01 12:58

もう一つのモデルで同じようにパスを修正したところ無事トップページに遷移し、テーブルにも情報の保存ができました! 本当にアドバイスありがとうございました!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問