teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

3

viewとバリデーションを編集、sharedを追加

2021/06/22 00:02

投稿

eminA
eminA

スコア4

title CHANGED
File without changes
body CHANGED
@@ -9,11 +9,6 @@
9
9
  ArgumentError in Scores#create
10
10
  'nil' is not an ActiveModel-compatible object. It must implement :to_partial_path.
11
11
  ```
12
- score.rbに付いているバリデーションを全て外して空欄で送信した場合のエラー
13
- ```errer
14
- ActiveRecord::NotNullViolation in ScoresController#create
15
- Mysql2::Error: Field 'score' doesn't have a default value
16
- ```
17
12
 
18
13
 
19
14
  ### 該当のソースコード
@@ -21,13 +16,14 @@
21
16
  ```ruby
22
17
  class Score < ApplicationRecord
23
18
  belongs_to :user
24
- VALID_SCORE_REGEX = /\A[0-9]+\z/
19
+ VALID_SCORE_REGEX = /\A(?:[1-9][0-9]{2,2}|0)(?:.[0-9]{1,1})?\z/ #修正した箇所
25
- validates :score, presence: true, format: { with: VALID_SCORE_REGEX }
20
+ validates_inclusion_of :score_before_type_cast, presence: true, in:400..650, format: { with: VALID_SCORE_REGEX }
26
21
  end
27
22
  ```
28
23
  index.html.erb
29
24
  ```ruby
30
25
  <%= form_with model: @score, class: 'form', local: true do |f| %>
26
+ <%= render 'shared/error_messages', model: f.object %> <% #追記した箇所 %>
31
27
  <div class="form-input">
32
28
  <%= f.number_field :score, step: "0.1", class: 'form-score', placeholder: 'type a score' %>
33
29
  <%= f.submit '送信', class: 'form-submit' %>
@@ -82,6 +78,18 @@
82
78
  add_foreign_key "scores", "users"
83
79
  end
84
80
  ```
81
+ shared/_error_messages.html.erb
82
+ ```ruby
83
+ <% if model.errors.any? %>
84
+ <div class="error-alert">
85
+ <ul>
86
+ <% model.errors.full_messages.each do |message| %>
87
+ <li class='error-message'><%= message %></li>
88
+ <% end %>
89
+ </ul>
90
+ </div>
91
+ <% end %>
92
+ ```
85
93
 
86
94
  ### 試したこと
87
95
 

2

schemaの追加

2021/06/22 00:02

投稿

eminA
eminA

スコア4

title CHANGED
File without changes
body CHANGED
@@ -53,7 +53,36 @@
53
53
  </div>
54
54
 
55
55
  ```
56
+ schema.rb
57
+ ```ruby
58
+ ActiveRecord::Schema.define(version: 2021_06_16_015118) do
56
59
 
60
+ create_table "scores", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t|
61
+ t.bigint "user_id"
62
+ t.decimal "score", precision: 4, scale: 1, null: false
63
+ t.decimal "score_avg", precision: 3, scale: 1
64
+ t.datetime "created_at", precision: 6, null: false
65
+ t.datetime "updated_at", precision: 6, null: false
66
+ t.index ["user_id"], name: "index_scores_on_user_id"
67
+ end
68
+
69
+ create_table "users", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t|
70
+ t.string "email", default: "", null: false
71
+ t.string "encrypted_password", default: "", null: false
72
+ t.string "nickname", default: "", null: false
73
+ t.string "reset_password_token"
74
+ t.datetime "reset_password_sent_at"
75
+ t.datetime "remember_created_at"
76
+ t.datetime "created_at", precision: 6, null: false
77
+ t.datetime "updated_at", precision: 6, null: false
78
+ t.index ["email"], name: "index_users_on_email", unique: true
79
+ t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
80
+ end
81
+
82
+ add_foreign_key "scores", "users"
83
+ end
84
+ ```
85
+
57
86
  ### 試したこと
58
87
 
59
88
  バリデーションを外した状態では空欄で送信すると上記エラーが出て、数字を入れた状態で送信するとエラーは出ません。

1

エラー詳細の追加

2021/06/21 09:21

投稿

eminA
eminA

スコア4

title CHANGED
File without changes
body CHANGED
@@ -4,16 +4,18 @@
4
4
  スコアを管理するアプリでスコアを送信するときに空欄で送信できないようにするためにバリデーションを設定しました。登録はできていませんがエラーになります。
5
5
  どのように解決できるでしょうか?教えてもらえると幸いです。
6
6
  ### 発生している問題・エラーメッセージ
7
-
7
+ バリデーション`validates :score, presence: true, format: { with: VALID_SCORE_REGEX }`を記述して空欄で送信した場合のエラー
8
8
  ```errer
9
9
  ArgumentError in Scores#create
10
10
  'nil' is not an ActiveModel-compatible object. It must implement :to_partial_path.
11
11
  ```
12
-
12
+ score.rbに付いているバリデーションを全て外して空欄で送信した場合のエラー
13
- ```params
13
+ ```errer
14
+ ActiveRecord::NotNullViolation in ScoresController#create
14
- {"authenticity_token"=>"0KoW0z0Ay61zOfES6cnc40XBIrdhc1r+7ByWhkRPqCem/HFgV3Wj6hB5v4vj3AnnZCXZkTC7Nzcj2X6Da8JQJA==", "score"=>{"score"=>""}, "commit"=>"送信"}
15
+ Mysql2::Error: Field 'score' doesn't have a default value
15
16
  ```
16
17
 
18
+
17
19
  ### 該当のソースコード
18
20
  score.rb
19
21
  ```ruby
@@ -24,7 +26,6 @@
24
26
  end
25
27
  ```
26
28
  index.html.erb
27
- この一番下の`<%= render @scores%>`がエラーとして指定されています。
28
29
  ```ruby
29
30
  <%= form_with model: @score, class: 'form', local: true do |f| %>
30
31
  <div class="form-input">