質問編集履歴

2

JSの記述を回答提示のリンクを参照し変更

2021/02/02 03:16

投稿

fastman
fastman

スコア3

test CHANGED
File without changes
test CHANGED
@@ -60,11 +60,7 @@
60
60
 
61
61
  ```javascript
62
62
 
63
- const rap = document.getElementById("rap");
64
-
65
-
66
-
67
- function addRap() {
63
+ function addRap() {
68
64
 
69
65
  const XHR = new XMLHttpRequest();
70
66
 
@@ -72,41 +68,21 @@
72
68
 
73
69
  XHR.open("POST", "/watches", true);
74
70
 
75
- XHR.responseType = "json";
71
+ XHR.setRequestHeader('content-type', 'application/json')
76
72
 
77
- XHR.send(time);
73
+ XHR.onreadystatechange = () => {
78
74
 
79
- XHR.onload = () => {
75
+ if (this.readyState === XMLHttpRequest.DONE && this.status === 200) {
80
76
 
81
- if (XHR.status != 200) {
77
+ const content = XHR.response;
82
78
 
83
- alert(`Error ${XHR.status}: ${XHR.statusText}`);
84
-
85
- return null;
79
+ console.log(content)
86
80
 
87
81
  }
88
82
 
89
- const content = XHR.response.watch;
83
+ };
90
84
 
91
- const table = document.getElementById("time-table");
92
-
93
- const HTML = `
94
-
95
- <tr>
96
-
97
- <div class="data" data-id=${content.id}>
98
-
99
- <td class="date">${content.id}</td>
85
+ XHR.send("watch="+time); //400(Bad Error)
100
-
101
- <td class="time">${content.watch}</td>
102
-
103
- </div>
104
-
105
- </tr>`;
106
-
107
- table.insertAdjacentHTML("beforeend", HTML);
108
-
109
- };
110
86
 
111
87
  }
112
88
 

1

htmlのid='time'の要素付加、ルーティングの追加、jsのaddEventListener追加

2021/02/02 03:16

投稿

fastman
fastman

スコア3

test CHANGED
File without changes
test CHANGED
@@ -19,6 +19,18 @@
19
19
 
20
20
 
21
21
  ```new
22
+
23
+ <%# タイマーの時間表示 %>
24
+
25
+ <div class="stop-watch">
26
+
27
+ <p id="time">00:00:00.000</p>
28
+
29
+ </div>
30
+
31
+
32
+
33
+ <%# STARTとSTOPのボタン %>
22
34
 
23
35
  <%= form_with url: watches_path, class: "buttons" do |f| %>
24
36
 
@@ -47,6 +59,10 @@
47
59
 
48
60
 
49
61
  ```javascript
62
+
63
+ const rap = document.getElementById("rap");
64
+
65
+
50
66
 
51
67
  function addRap() {
52
68
 
@@ -78,11 +94,11 @@
78
94
 
79
95
  <tr>
80
96
 
81
- <td class="time">
97
+ <div class="data" data-id=${content.id}>
82
98
 
83
- ${content.watch}
99
+ <td class="date">${content.id}</td>
84
100
 
85
- </td>
101
+ <td class="time">${content.watch}</td>
86
102
 
87
103
  </div>
88
104
 
@@ -93,6 +109,34 @@
93
109
  };
94
110
 
95
111
  }
112
+
113
+
114
+
115
+ rap.addEventListener('click', (e) => {
116
+
117
+ addRap();
118
+
119
+ e.preventDefault();
120
+
121
+ }
122
+
123
+ });
124
+
125
+ ```
126
+
127
+
128
+
129
+ ```routes
130
+
131
+ Rails.application.routes.draw do
132
+
133
+ devise_for :users
134
+
135
+
136
+
137
+ resources :watches, only: [:index, :new, :create]
138
+
139
+ end
96
140
 
97
141
  ```
98
142