質問編集履歴
3
viewとバリデーションを編集、sharedを追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -20,16 +20,6 @@
|
|
20
20
|
|
21
21
|
```
|
22
22
|
|
23
|
-
score.rbに付いているバリデーションを全て外して空欄で送信した場合のエラー
|
24
|
-
|
25
|
-
```errer
|
26
|
-
|
27
|
-
ActiveRecord::NotNullViolation in ScoresController#create
|
28
|
-
|
29
|
-
Mysql2::Error: Field 'score' doesn't have a default value
|
30
|
-
|
31
|
-
```
|
32
|
-
|
33
23
|
|
34
24
|
|
35
25
|
|
@@ -44,9 +34,9 @@
|
|
44
34
|
|
45
35
|
belongs_to :user
|
46
36
|
|
47
|
-
VALID_SCORE_REGEX = /\A[0-9]
|
37
|
+
VALID_SCORE_REGEX = /\A(?:[1-9][0-9]{2,2}|0)(?:.[0-9]{1,1})?\z/ #修正した箇所
|
48
|
-
|
38
|
+
|
49
|
-
validates :score, presence: true, format: { with: VALID_SCORE_REGEX }
|
39
|
+
validates_inclusion_of :score_before_type_cast, presence: true, in:400..650, format: { with: VALID_SCORE_REGEX }
|
50
40
|
|
51
41
|
end
|
52
42
|
|
@@ -58,6 +48,8 @@
|
|
58
48
|
|
59
49
|
<%= form_with model: @score, class: 'form', local: true do |f| %>
|
60
50
|
|
51
|
+
<%= render 'shared/error_messages', model: f.object %> <% #追記した箇所 %>
|
52
|
+
|
61
53
|
<div class="form-input">
|
62
54
|
|
63
55
|
<%= f.number_field :score, step: "0.1", class: 'form-score', placeholder: 'type a score' %>
|
@@ -166,6 +158,30 @@
|
|
166
158
|
|
167
159
|
```
|
168
160
|
|
161
|
+
shared/_error_messages.html.erb
|
162
|
+
|
163
|
+
```ruby
|
164
|
+
|
165
|
+
<% if model.errors.any? %>
|
166
|
+
|
167
|
+
<div class="error-alert">
|
168
|
+
|
169
|
+
<ul>
|
170
|
+
|
171
|
+
<% model.errors.full_messages.each do |message| %>
|
172
|
+
|
173
|
+
<li class='error-message'><%= message %></li>
|
174
|
+
|
175
|
+
<% end %>
|
176
|
+
|
177
|
+
</ul>
|
178
|
+
|
179
|
+
</div>
|
180
|
+
|
181
|
+
<% end %>
|
182
|
+
|
183
|
+
```
|
184
|
+
|
169
185
|
|
170
186
|
|
171
187
|
### 試したこと
|
2
schemaの追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -108,6 +108,64 @@
|
|
108
108
|
|
109
109
|
```
|
110
110
|
|
111
|
+
schema.rb
|
112
|
+
|
113
|
+
```ruby
|
114
|
+
|
115
|
+
ActiveRecord::Schema.define(version: 2021_06_16_015118) do
|
116
|
+
|
117
|
+
|
118
|
+
|
119
|
+
create_table "scores", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t|
|
120
|
+
|
121
|
+
t.bigint "user_id"
|
122
|
+
|
123
|
+
t.decimal "score", precision: 4, scale: 1, null: false
|
124
|
+
|
125
|
+
t.decimal "score_avg", precision: 3, scale: 1
|
126
|
+
|
127
|
+
t.datetime "created_at", precision: 6, null: false
|
128
|
+
|
129
|
+
t.datetime "updated_at", precision: 6, null: false
|
130
|
+
|
131
|
+
t.index ["user_id"], name: "index_scores_on_user_id"
|
132
|
+
|
133
|
+
end
|
134
|
+
|
135
|
+
|
136
|
+
|
137
|
+
create_table "users", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t|
|
138
|
+
|
139
|
+
t.string "email", default: "", null: false
|
140
|
+
|
141
|
+
t.string "encrypted_password", default: "", null: false
|
142
|
+
|
143
|
+
t.string "nickname", default: "", null: false
|
144
|
+
|
145
|
+
t.string "reset_password_token"
|
146
|
+
|
147
|
+
t.datetime "reset_password_sent_at"
|
148
|
+
|
149
|
+
t.datetime "remember_created_at"
|
150
|
+
|
151
|
+
t.datetime "created_at", precision: 6, null: false
|
152
|
+
|
153
|
+
t.datetime "updated_at", precision: 6, null: false
|
154
|
+
|
155
|
+
t.index ["email"], name: "index_users_on_email", unique: true
|
156
|
+
|
157
|
+
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
|
158
|
+
|
159
|
+
end
|
160
|
+
|
161
|
+
|
162
|
+
|
163
|
+
add_foreign_key "scores", "users"
|
164
|
+
|
165
|
+
end
|
166
|
+
|
167
|
+
```
|
168
|
+
|
111
169
|
|
112
170
|
|
113
171
|
### 試したこと
|
1
エラー詳細の追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -10,7 +10,7 @@
|
|
10
10
|
|
11
11
|
### 発生している問題・エラーメッセージ
|
12
12
|
|
13
|
-
|
13
|
+
バリデーション`validates :score, presence: true, format: { with: VALID_SCORE_REGEX }`を記述して空欄で送信した場合のエラー
|
14
14
|
|
15
15
|
```errer
|
16
16
|
|
@@ -20,13 +20,17 @@
|
|
20
20
|
|
21
21
|
```
|
22
22
|
|
23
|
+
score.rbに付いているバリデーションを全て外して空欄で送信した場合のエラー
|
24
|
+
|
25
|
+
```errer
|
26
|
+
|
27
|
+
ActiveRecord::NotNullViolation in ScoresController#create
|
28
|
+
|
29
|
+
Mysql2::Error: Field 'score' doesn't have a default value
|
30
|
+
|
31
|
+
```
|
23
32
|
|
24
33
|
|
25
|
-
```params
|
26
|
-
|
27
|
-
{"authenticity_token"=>"0KoW0z0Ay61zOfES6cnc40XBIrdhc1r+7ByWhkRPqCem/HFgV3Wj6hB5v4vj3AnnZCXZkTC7Nzcj2X6Da8JQJA==", "score"=>{"score"=>""}, "commit"=>"送信"}
|
28
|
-
|
29
|
-
```
|
30
34
|
|
31
35
|
|
32
36
|
|
@@ -49,8 +53,6 @@
|
|
49
53
|
```
|
50
54
|
|
51
55
|
index.html.erb
|
52
|
-
|
53
|
-
この一番下の`<%= render @scores%>`がエラーとして指定されています。
|
54
56
|
|
55
57
|
```ruby
|
56
58
|
|