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

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

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

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

Ruby on Rails 6

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

Q&A

解決済

1回答

651閲覧

devisesをカスタムしたログイン機能の実装中のエラー

stmaho

総合スコア1

Devise

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

Ruby on Rails 6

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

0グッド

0クリップ

投稿2023/04/23 07:59

編集2023/04/23 21:13

実現したいこと

・deviseを利用したログインをemail・passwordを使用せずに別途発行されているID(6桁程度のランダムな数字)と生年月日(8桁)をログインのキーとしたい。
現在、下記のようなエラーが出ておりDBに保存ができません。

前提

管理者によって各ユーザーに登録された動画の一覧ページを見られる、アプリを作成中。
ユーザー登録には上記記載の通り、各自発行されているIDと生年月日をID/passとして利用したいと思っていますが、そのユーザー登録機能実装中にエラーが出てしまいてこずっております。

開発環境

rails :ver.6.0.0
VSコード

発生している問題・エラーメッセージ

フォームからID/生年月日を入力し、新規登録ボタンを押した時点でのlogです。

Started POST "/users" for ::1 at 2023-04-23 16:34:49 +0900 Processing by Users::RegistrationsController#create as JS Parameters: {"authenticity_token"=>"WygREmMls0QOiW8B4Vvu3KXwui8N+vdxA32VEEFNFFjiboS+7HlOcaP5B0R49N5+Qj1Xk7Tbdd50b9mfTx3zfQ==", "user"=>{"patient_id"=>"510000", "birth_date"=>"19990101"}, "commit"=>"新規登録"} Rendering devise/registrations/new.html.erb within layouts/application Rendered devise/shared/_links.html.erb (Duration: 0.2ms | Allocations: 89) Rendered devise/registrations/new.html.erb within layouts/application (Duration: 1.6ms | Allocations: 1082) [Webpacker] Everything's up-to-date. Nothing to do Completed 422 Unprocessable Entity in 13ms (Views: 10.2ms | ActiveRecord: 0.0ms | Allocations: 8613)

該当のソースコード

registration_controller.rb

1class Users::RegistrationsController < Devise::RegistrationsController 2 before_action :configure_sign_up_params, if: :devise_controller? 3 4 5 private 6 def configure_sign_up_params 7 devise_parameter_sanitizer.permit(:sign_up, keys: [:patient_id, :birth_date]) 8 end 9 10end 11

user.rb

1class User < ApplicationRecord 2 # Include default devise modules. Others available are: 3 # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable 4 devise :database_authenticatable, :registerable, 5 :recoverable, :rememberable, :validatable 6 7 validates :patient_id, presence: true 8 validates :birth_date, presence: true, format: { with: /\A[0-9]{8}\z/, message: 'is invalid' } 9 10 has_many :exercises 11end 12

new.html.erb

1<h2>ID/passを入力してください</h2> 2 3<%= form_with model: @user, url: user_registration_path, method: :post do |f| %> 4 5<div class= "contents"> 6 <div class="form-group"> 7 <%= f.text_field :patient_id, class: 'form-control', placeholder:"例) 51XXXXXX" %> 8 <%= f.text_field :birth_date, class: 'form-control', placeholder:"生年月日8桁 20100101" %> 9 </div> 10 11 <div class="actions"> 12 <%= f.submit '新規登録', class: 'btn btn-primary' %> 13 </div> 14</div> 15<% end %> 16 17<%= render "devise/shared/links" %>

devise.rb

1 config.authentication_keys = [:patient_id, :birth_date]

試したこと

binding.pryも導入しましたが、使い方も深く理解でていないため、使いこなせておりません。
どこに挿入し、コンソールになにを記入すれば良いか分からず。。。
パラメーターなどの記述に間違いはないと思うのですが、なにがエラーに繋がっているのかわかりません。
初歩的な質問で申し訳ありませんが、どなたかアドバイス頂けると幸いです。

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

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

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

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

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

winterboum

2023/04/23 10:23

Devise って password データなしで動くのかな。 rails c にて user = User.create(patient_id: "510000", birth_date: "19990101") user.errors.messges してみてください。なぜ Rendering devise/registrations/new.html.erb に行ってしまうのかわかるかも
winterboum

2023/04/23 10:24

あと config/initializers/devise.rb をどう修正したか、も載せて下さい
stmaho

2023/04/23 10:47

ご質問ありがとうございます。 >>Devise って password データなしで動くのかな。 生年月日の8桁をパスワードとしたいと思っていました。 カラムの設定などはパスワードにして、フロントで生年月日の入力を促す形の方が良いでしょうか。 rails cにてご指摘いただいたことをしてみました。 [1] pry(main)> user = User.create(patient_id: "510000", birth_date: "19990101") => #<User id: nil, email: "", created_at: nil, updated_at: nil, patient_id: "510000", birth_date: "1999-01-01"> [2] pry(main)> user.errors.messges NoMethodError: undefined method `messges' for #<ActiveModel::Errors:0x00007fd420a0fe28> Did you mean? messages from (pry):2:in `__pry__' [3] pry(main)> user.errors.messages => {:email=>["can't be blank"], :password=>["can't be blank"], :birth_date=>["is invalid"]} config/initializers/devise.rbについては config.authentication_keys = [:patient_id, :birth_date] こちらを追記しております。
guest

回答1

0

自己解決

user.rb

1 devise :database_authenticatable, :registerable, 2 :recoverable, :rememberable 3 4:validatable の削除

migrationファイル

1class AddPatientIdAndBirthDateToUsers < ActiveRecord::Migration[6.0] 2 def change 3 add_column :users, :patient_id, :string, null: false, unique: true 4 add_column :users, :birth_date, :string, null: false 5 end 6end 7 8birth_dateのカラムタイプを:date→:stringに変更

上記の修正でユーザー登録が可能となりました。

1user.rb
deviseのデフォルト設定のemail,passwordを使用しないのであればデフォルトのバリデーションも外さなくてはならない。

2.birth_dateカラムのタイプ変更
生年月日というだけで、:dataとしておりましたが、使用したいのは8桁の数字であり
バリデーションを

validates :birth_date, presence: true, format: { with: /\A[0-9]{8}\z/, message: 'is invalid' }

としていため、弾かれていました。
winterboumさんがコンソールの操作を記述していただいたおかげで、気づくことができました。
ありがとうございました!!

投稿2023/04/23 12:11

編集2023/04/23 12:13
stmaho

総合スコア1

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問