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

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

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

Vue.jsは、Webアプリケーションのインターフェースを構築するためのオープンソースJavaScriptフレームワークです。

Ruby on Rails

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

バリデーション

Validationとは特定の入力データが、求められた条件に当てまっているかをチェックするために使われます。

Q&A

解決済

1回答

786閲覧

なぜパスワードのバリデーションエラーが発生するのか解決したい

poo3dayo

総合スコア0

Vue.js

Vue.jsは、Webアプリケーションのインターフェースを構築するためのオープンソースJavaScriptフレームワークです。

Ruby on Rails

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

バリデーション

Validationとは特定の入力データが、求められた条件に当てまっているかをチェックするために使われます。

0グッド

0クリップ

投稿2021/05/03 12:40

前提・実現したいこと

コントローラー内のUser.save時にパスワードのバリデーションエラーが発生する
VueテンプレートからPOSTでParamsをUsersコントローラに送っています

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

log

1web_1 | 10:55:08 web.1 | Started POST "/users" for 172.22.0.1 at 2021-05-03 10:55:08 +0000 2web_1 | 10:55:08 web.1 | Cannot render console from 172.22.0.1! Allowed networks: 127.0.0.0/127.255.255.255, ::1 3web_1 | 10:55:09 web.1 | Processing by UsersController#create as */* 4web_1 | 10:55:09 web.1 | Parameters: {"name"=>"先生", "email"=>"teacher07540503@gmail.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "user_type"=>"teacher", "user"=>{"name"=>"先生", "email"=>"teacher07540503@gmail.com", "user_type"=>"teacher"}} 5web_1 | 10:55:09 web.1 | DEPRECATION WARNING: Uniqueness validator will no longer enforce case sensitive comparison in Rails 6.1. To continue case sensitive comparison on the :email attribute in User model, pass `case_sensitive: true` option explicitly to the uniqueness validator. (called from create at /myapp/app/controllers/users_controller.rb:31) 6web_1 | 10:55:09 web.1 | (0.6ms) BEGIN 7web_1 | 10:55:09 web.1 | ↳ app/controllers/users_controller.rb:31:in `create' 8web_1 | 10:55:09 web.1 | User Exists? (0.9ms) SELECT 1 AS one FROM `users` WHERE `users`.`email` = BINARY 'teacher07540503@gmail.com' LIMIT 1 9web_1 | 10:55:09 web.1 | ↳ app/controllers/users_controller.rb:31:in `create' 10web_1 | 10:55:09 web.1 | (0.5ms) ROLLBACK 11web_1 | 10:55:09 web.1 | ↳ app/controllers/users_controller.rb:31:in `create' 12web_1 | 10:55:09 web.1 | Completed 422 Unprocessable Entity in 28ms (Views: 0.3ms | ActiveRecord: 7.9ms | Allocations: 9206)

エラー時のアプリケーション画面

ruby

1class User < ApplicationRecord 2 has_many :questions, dependent: :destroy 3 attr_accessor :remember_token 4 before_save {email.downcase!} 5 validates :name,presence: true,length: {maximum: 50} 6 VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+.[a-z]+\z/i 7 validates :email,presence: true,length: {maximum: 255}, 8 format: {with: VALID_EMAIL_REGEX}, 9 uniqueness: true 10 has_secure_password 11 validates :password,presence: true,length:{minimum: 8},allow_nil: true 12 validates :user_type,presence: true 13 14 class << self 15 # 渡された文字列のハッシュ値を返す 16 def digest(string) 17 cost = ActiveModel::SecurePassword.min_cost ? BCrypt::Engine::MIN_COST : 18 BCrypt::Engine.cost 19 BCrypt::Password.create(string, cost: cost) 20 end 21end 22

該当のソースコード

ruby

1def create 2 user = User.new(user_params) 3 if user.save 4 log_in user 5 render json: { 6 user: user, 7 message: "#{user.name}様ようこそPleaseTeachmeへ", 8 }, 9 status: :created 10 else 11 render json: { 12 errors: user.errors.full_messages, 13 }, 14 status: :unprocessable_entity 15 end 16end 17 18def user_params 19 params 20 .require(:user) 21 .permit(:name, :email, :password, :password_confirmation, :user_type) 22end

vue

1<template> 2 <div class="user-form-wrapper"> 3 <div v-if="errors.length != 0"> 4 <ul v-for="e in errors" :key="e"> 5 <li class="vlidate-message"> 6 {{ e }} 7 </li> 8 </ul> 9 </div> 10 <div class="form-group"> 11 <label for="userName">名前</label> 12 <input 13 type="text" 14 name="userName" 15 v-model="user.name" 16 class="form-control" 17 id="userName" 18 placeholder="名前を入力して下さい" 19 /> 20 </div> 21 <div class="form-group"> 22 <label for="userEmail">メールアドレス</label> 23 <input 24 type="email" 25 name="userEmail" 26 v-model="user.email" 27 class="form-control" 28 id="userEmail" 29 placeholder="メールアドレスを入力して下さい" 30 /> 31 </div> 32 <div class="form-group"> 33 <label for="userPassword">パスワード</label> 34 <input 35 type="password" 36 name="userPassword" 37 v-model="user.password" 38 class="form-control" 39 id="userPassword" 40 placeholder="パスワードを入力して下さい" 41 /> 42 </div> 43 <div class="form-group"> 44 <label for="userPasswordConfirmation">パスワードの確認</label> 45 <input 46 type="password" 47 name="userPasswordConfirmation" 48 v-model="user.password_confirmation" 49 class="form-control" 50 id="userPasswordConfirmation" 51 placeholder="再度パスワード入力して下さい" 52 /> 53 </div> 54 <div class="submit-button-wrapper"> 55 <input 56 @click="submitUser" 57 type="submit" 58 value="送信" 59 name="submitBtn" 60 class="btn btn-submit" 61 id="submitBtn" 62 /> 63 </div> 64 </div> 65</template> 66 67<script> 68import axios from "axios"; 69axios.defaults.headers.common = { 70 "X-Requested-With": "XMLHttpRequest", 71 "X-CSRF-TOKEN": document 72 .querySelector('meta[name="csrf-token"]') 73 .getAttribute("content"), 74}; 75export default { 76 data() { 77 return { 78 errors: "", 79 user: { 80 name: "", 81 email: "", 82 password: "", 83 password_confirmation: "", 84 user_type: "teacher", 85 }, 86 }; 87 }, 88 methods: { 89 submitUser() { 90 axios 91 .post("/users", this.user) 92 .then((response) => { 93 console.log(response); 94 const createdUser = response.data.user; 95 this.$store.dispatch("catchMessage", { 96 message: response.data.message, 97 timeout: 5000, 98 }); 99 this.$store.dispatch("changeLogin"); 100 this.$router.push({ 101 name: "teachers_user_show_path", 102 params: { id: createdUser.id }, 103 }); 104 }) 105 .catch((error) => { 106 console.error(error); 107 if (error.response.data && error.response.data.errors) { 108 this.errors = error.response.data.errors; 109 } 110 }); 111 }, 112 }, 113}; 114</script>

試したこと

リクエストスペックやRailsコンソールからはうまく保存できます(正しくコントローラーが動いている)

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

Rails6.0

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

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

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

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

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

guest

回答1

0

自己解決

APIのオブジェクトとしてthis.userとしていた部分をユーザという名前のオブジェクトを持つパラメータにしたら解決しました!!

{post "/users",this.user}=>{user:{ユーザのパラメータ}}

投稿2021/05/03 15:08

poo3dayo

総合スコア0

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問