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

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

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

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

Ruby on Rails 6

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

Q&A

解決済

1回答

1607閲覧

[Ruby on Rails]Deviseでフォームから新規登録ができない。submitしてもPOSTにならず、GETになってしまう

hajsu00

総合スコア151

Devise

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

Ruby on Rails 6

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

0グッド

0クリップ

投稿2022/04/02 03:56

お世話になっております。

既存アプリにDeviseでユーザー認証機能を実装中です。Deviseでフォームから新規登録ができない現象を解決できずにいます。

ご助言いただけると幸いです。

不具合内容

SubmitしてもPOSTにならず、GETが送信されてしまうためアカウント登録画面が再度表示されてしまいます。

console

1Started GET "/users/sign_up" for ::1 at 2022-04-02 12:36:22 +0900 2Processing by Devise::RegistrationsController#new as HTML 3 Rendering layout layouts/application.html.erb 4 Rendering devise/registrations/new.html.erb within layouts/application 5 Rendered devise/shared/_error_messages.html.erb (Duration: 1.7ms | Allocations: 242) 6 Rendered devise/registrations/new.html.erb within layouts/application (Duration: 8.5ms | Allocations: 1825) 7[Webpacker] Everything's up-to-date. Nothing to do 8 Rendered layouts/_shim.html.erb (Duration: 0.2ms | Allocations: 40) 9 Rendered layouts/_header.html.erb (Duration: 1.0ms | Allocations: 125) 10 Rendered layouts/_sidebar.html.erb (Duration: 0.5ms | Allocations: 52) 11 Rendered layouts/_footer.html.erb (Duration: 0.2ms | Allocations: 40) 12 Rendered layout layouts/application.html.erb (Duration: 49.9ms | Allocations: 14432) 13Completed 200 OK in 63ms (Views: 59.2ms | ActiveRecord: 0.0ms | Allocations: 16257)

前提事項

・macOS Monterey v12.1
・ruby v3.0.3
・Rails v6.1.4.4
・devise 4.8.1

その他
・自作ユーザー認証機能のdeviseへの置き換え
・Divise用のコントローラとViewを作成済み
・コンソールからのユーザー登録は正常に動作
・logにはデータベースと通信した形跡は無し(次項参照)

関係するコード

ルーティング

config/routes.rb

1 devise_for :users, controllers: { 2 registrations: 'users/registrations', 3 sessions: 'users/sessions', 4 passwords: 'users/passwords', 5 confirmations: 'users/confirmations' 6 } 7 8 devise_scope :user do 9 root "devise/sessions#new" 10 end 11...

console

1$ rails routes 2 3 new_user_session GET /users/sign_in(.:format) users/sessions#new 4 user_session POST /users/sign_in(.:format) users/sessions#create 5 destroy_user_session DELETE /users/sign_out(.:format) users/sessions#destroy 6 new_user_password GET /users/password/new(.:format) users/passwords#new 7 edit_user_password GET /users/password/edit(.:format) users/passwords#edit 8 user_password PATCH /users/password(.:format) users/passwords#update 9 PUT /users/password(.:format) users/passwords#update 10 POST /users/password(.:format) users/passwords#create 11 cancel_user_registration GET /users/cancel(.:format) users/registrations#cancel 12 new_user_registration GET /users/sign_up(.:format) users/registrations#new 13 edit_user_registration GET /users/edit(.:format) users/registrations#edit 14 user_registration PATCH /users(.:format) users/registrations#update 15 PUT /users(.:format) users/registrations#update 16 DELETE /users(.:format) users/registrations#destroy 17 POST /users(.:format) users/registrations#create 18 root GET / devise/sessions#new

コントローラー

app/controllers/users/registrations_controller.rb

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

###ビュー

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

1<% provide(:title, 'アカウント登録') %> 2<% provide(:button_text, '認証メールを送信する') %> 3 4 <div class="bg-white py-6 sm:py-8 lg:py-12"> 5 <div class="max-w-screen-2xl px-4 md:px-8 mx-auto"> 6 <h2 class="text-gray-800 text-2xl lg:text-3xl font-bold text-center mb-4 md:mb-8">アカウント登録</h2> 7 <form class="max-w-lg border rounded-lg mx-auto"> 8 <div class="flex flex-col gap-4 p-4 md:p-8"> 9 <%= form_with model: @user, url: user_registration_path, id: 'new_user', class: 'new_user', local: true do |f| %> 10 <%= render "devise/shared/error_messages", resource: @user %> 11 <div> 12 <%= f.label :name, "アカウント名", class: 'inline-block text-gray-800 text-sm sm:text-base mb-2' %> 13 <%= f.text_field :name, autofocus: true, autocomplete: "name", class: 'w-full bg-gray-50 text-gray-800 border focus:ring ring-indigo-300 rounded outline-none transition duration-100 px-3 py-2' %> 14 </div> 15 <div> 16 <%= f.label :email, "メールアドレス", class: 'inline-block text-gray-800 text-sm sm:text-base mb-2' %> 17 <%= f.email_field :email, autofocus: true, autocomplete: "email", class: 'w-full bg-gray-50 text-gray-800 border focus:ring ring-indigo-300 rounded outline-none transition duration-100 px-3 py-2' %> 18 </div> 19 <div> 20 <%= f.label :password, "パスワード", class: 'inline-block text-gray-800 text-sm sm:text-base mb-2' %> 21 <% if @minimum_password_length %> 22 <em>(<%= @minimum_password_length %> characters minimum)</em> 23 <% end %><br /> 24 <%= f.password_field :password, autocomplete: "new-password", class: 'w-full bg-gray-50 text-gray-800 border focus:ring ring-indigo-300 rounded outline-none transition duration-100 px-3 py-2' %> 25 </div> 26 <div> 27 <%= f.label :password_confirmation, "パスワード(確認)", class: 'inline-block text-gray-800 text-sm sm:text-base mb-2' %> 28 <%= f.password_field :password_confirmation, autocomplete: "new-password", class: 'w-full bg-gray-50 text-gray-800 border focus:ring ring-indigo-300 rounded outline-none transition duration-100 px-3 py-2' %> 29 </div> 30 <!-- 認証メール送信ボタン --> 31 <%= f.submit yield(:button_text), class: "block bg-gray-800 hover:bg-gray-700 active:bg-gray-600 focus-visible:ring ring-gray-300 text-white text-sm md:text-base font-semibold text-center rounded-lg outline-none transition duration-100 px-8 py-3" %> 32 <% end %> 33 </div> 34 <!-- ナビゲーション --> 35 <div class="flex justify-center items-center p-4 "> 36 <%= link_to "アカウントをお持ちの方はこちら", new_user_session_path, class: "text-indigo-500 hover:text-indigo-600 active:text-indigo-700 transition duration-100" %> 37 </div> 38 </form> 39 </div> 40</div>

何か、考えられる原因はありますでしょうか。
よろしくお願いします。

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

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

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

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

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

guest

回答1

0

自己解決

結論

viewにformタグが記載されていたのが原因。
divタグへ変更したところ、正常にPOSTが動作しました。

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

1 2<% provide(:title, 'アカウント登録') %> 3<% provide(:button_text, '認証メールを送信する') %> 4 5 <div class="bg-white py-6 sm:py-8 lg:py-12"> 6 <div class="max-w-screen-2xl px-4 md:px-8 mx-auto"> 7 <h2 class="text-gray-800 text-2xl lg:text-3xl font-bold text-center mb-4 md:mb-8">アカウント登録</h2> 8 <form class="max-w-lg border rounded-lg mx-auto"> <= <div>タグに変える 9 <div class="flex flex-col gap-4 p-4 md:p-8"> 10 <%= form_with model: @user, url: user_registration_path, id: 'new_user', class: 'new_user', local: true do |f| %> 11 <%= render "devise/shared/error_messages", resource: @user %> 12 <div> 13 <%= f.label :name, "アカウント名", class: 'inline-block text-gray-800 text-sm sm:text-base mb-2' %> 14 <%= f.text_field :name, autofocus: true, autocomplete: "name", class: 'w-full bg-gray-50 text-gray-800 border focus:ring ring-indigo-300 rounded outline-none transition duration-100 px-3 py-2' %> 15 </div> 16 <div> 17 <%= f.label :email, "メールアドレス", class: 'inline-block text-gray-800 text-sm sm:text-base mb-2' %> 18 <%= f.email_field :email, autofocus: true, autocomplete: "email", class: 'w-full bg-gray-50 text-gray-800 border focus:ring ring-indigo-300 rounded outline-none transition duration-100 px-3 py-2' %> 19 </div> 20 <div> 21 <%= f.label :password, "パスワード", class: 'inline-block text-gray-800 text-sm sm:text-base mb-2' %> 22 <% if @minimum_password_length %> 23 <em>(<%= @minimum_password_length %> characters minimum)</em> 24 <% end %><br /> 25 <%= f.password_field :password, autocomplete: "new-password", class: 'w-full bg-gray-50 text-gray-800 border focus:ring ring-indigo-300 rounded outline-none transition duration-100 px-3 py-2' %> 26 </div> 27 <div> 28 <%= f.label :password_confirmation, "パスワード(確認)", class: 'inline-block text-gray-800 text-sm sm:text-base mb-2' %> 29 <%= f.password_field :password_confirmation, autocomplete: "new-password", class: 'w-full bg-gray-50 text-gray-800 border focus:ring ring-indigo-300 rounded outline-none transition duration-100 px-3 py-2' %> 30 </div> 31 <!-- 認証メール送信ボタン --> 32 <%= f.submit yield(:button_text), class: "block bg-gray-800 hover:bg-gray-700 active:bg-gray-600 focus-visible:ring ring-gray-300 text-white text-sm md:text-base font-semibold text-center rounded-lg outline-none transition duration-100 px-8 py-3" %> 33 <% end %> 34 </div> 35 <!-- ナビゲーション --> 36 <div class="flex justify-center items-center p-4 "> 37 <%= link_to "アカウントをお持ちの方はこちら", new_user_session_path, class: "text-indigo-500 hover:text-indigo-600 active:text-indigo-700 transition duration-100" %> 38 </div> 39 </form> <= </div>タグに変える 40 </div> 41</div>

投稿2022/04/02 14:01

hajsu00

総合スコア151

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問