質問編集履歴
1
コードのスクリーンショットでは、回答者の方への配慮が足りないと思い、コードの記述に変更しました。
title
CHANGED
File without changes
|
body
CHANGED
@@ -12,15 +12,62 @@
|
|
12
12
|
|
13
13
|
### 現在のVsコードの記述↓
|
14
14
|
<マイグレーション>
|
15
|
+
```ruby
|
16
|
+
class CreateForms < ActiveRecord::Migration[6.0]
|
17
|
+
def change
|
18
|
+
create_table :forms do |t|
|
19
|
+
t.string :last_name_kana, null: false
|
20
|
+
t.string :first_name_kana, null: false
|
21
|
+
t.string :phone_number, null: false
|
22
|
+
t.string :number_of_people, null: false
|
15
|
-
|
23
|
+
t.integer :seat, null: false, default: 0
|
24
|
+
t.datetime :datetime, null: false
|
16
25
|
|
26
|
+
t.timestamps
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
```
|
17
32
|
<コントローラー>
|
33
|
+
```ruby
|
18
|
-
|
34
|
+
class FormsController < ApplicationController
|
35
|
+
|
36
|
+
def index
|
37
|
+
@form = Form.new
|
38
|
+
end
|
19
39
|
|
40
|
+
def create
|
41
|
+
@form = Form.new(form_params)
|
42
|
+
|
43
|
+
end
|
44
|
+
|
45
|
+
private
|
46
|
+
|
47
|
+
def form_params
|
48
|
+
params.require(:form).permit(:last_name_kana, :first_name_kana, :phone_number, :number_of_people, :seat, :datetime)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
```
|
52
|
+
|
20
53
|
<モデル>
|
21
|
-
|
54
|
+
```ruby
|
22
|
-
|
55
|
+
class Form < ApplicationRecord
|
56
|
+
enum seat: { table: 0, counter: 1}
|
23
57
|
|
58
|
+
with_options presence: true do
|
59
|
+
validates :last_name_kana, format: { with: /\A[ァ-ヶー-]+\z/, message: 'is invalid. Input full-width characters.'}
|
60
|
+
validates :first_name_kana, format: { with: /\A[ァ-ヶー-]+\z/, message: 'is invalid. Input full-width characters.'}
|
61
|
+
validates :number_of_people, numericality: { only_integer: true, message: 'is invalid. Input only number'}
|
62
|
+
validates :datetime
|
63
|
+
end
|
64
|
+
|
65
|
+
VALID_PHONE_REGEX = /\A\d{10}$|^\d{11}\z/
|
66
|
+
validates :phone_number, presence: true, format: { with: VALID_PHONE_REGEX }
|
67
|
+
validates :phone_number, numericality: { only_integer: true, message: 'is invalid. Input only number' }
|
68
|
+
|
69
|
+
end
|
70
|
+
```
|
24
71
|
### enumを用いるために行った記述
|
25
72
|
1つめ
|
26
73
|
```ruby:form.rb
|
@@ -35,11 +82,14 @@
|
|
35
82
|
bund installしました
|
36
83
|
|
37
84
|
3つめ
|
85
|
+
```ruby:config>locales>ja.yml
|
38
|
-
|
86
|
+
ja:
|
87
|
+
enums:
|
88
|
+
form:
|
89
|
+
seat:
|
90
|
+
table: テーブル席
|
39
|
-
|
91
|
+
counter: カウンター席
|
40
|
-
|
92
|
+
```
|
41
|
-
|
42
|
-
|
43
93
|
#### 今回の問題点
|
44
94
|
・予約フォームへ遷移するとき、保存するときにエラーが出る
|
45
95
|
・リロードすると、エラーが出なくなり、DBに保存もできる
|