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

質問編集履歴

2

記述の変更

2021/01/13 17:27

投稿

yasunsun
yasunsun

スコア2

title CHANGED
File without changes
body CHANGED
@@ -4,20 +4,22 @@
4
4
  アドバイスを頂けると嬉しいです。
5
5
 
6
6
  ###実現したいこと
7
- `topicId`の値を取得したいです。
7
+ `post.js`で`topicId`の値を取得したいです。
8
+ `posts/index`8行目の`<div class="post" data-id = <%= @topic.id %>>`からidを渡します。
8
9
 
9
10
  ・post.js 4~8行目
10
11
  ```JavaScript
11
12
  const posts = document.querySelectorAll(".post");
12
- const topicId = posts.getAttribute("data-id");
13
+ const topicId = posts.getAttribute("data-id");
13
- const formData = new FormData(document.getElementById("form"));
14
+ const formData = new FormData(document.getElementById("form"));
14
- const XHR = new XMLHttpRequest();
15
+ const XHR = new XMLHttpRequest();
15
- XHR.open("POST", `/topics/${topicId}/posts`, true);
16
+ XHR.open("POST", `/topics/${topicId}/posts`, true);
16
17
  ```
17
18
 
18
19
  ### 発生している問題・エラーメッセージ
19
- ・post.js 8行目
20
+ ・post.js 4, 5行目
21
+ `const posts = document.querySelectorAll(".post");`で`class = post`を取得して、
20
- クラスのpostを取得して、そこからdata-idを取得しようと試みました。
22
+ そこから`const topicId = posts.getAttribute("data-id");`で`data-id`を取得しようと試みました。
21
23
  しかし、`topicId`の値が取得できずコンソールにエラーが表示されます。
22
24
  ```Console
23
25
  Uncaught TypeError: post.getAttribute is not a function

1

タイトル、記述の変更

2021/01/13 17:27

投稿

yasunsun
yasunsun

スコア2

title CHANGED
@@ -1,1 +1,1 @@
1
- 【Ruby】掲示板アプリの非同期通信を可能にしたいです
1
+ 【Ruby, JavaScript】掲示板アプリの非同期通信を可能にしたいです
body CHANGED
@@ -1,21 +1,27 @@
1
- ### 前提・実現したいこと
1
+ ### 前提
2
2
  `Ruby on Rails 6.0.0`で掲示板アプリを作っています。
3
3
  非同期通信でコメントを投稿できるようにしたいです。
4
4
  アドバイスを頂けると嬉しいです。
5
5
 
6
- ### 発生る問題・エラーメッセージ
6
+ ###実現こと
7
- `Topic`と`Post`はルーティングでネストを組んでいます。
8
- 送信ボタンを押すと以下のエラーが表示されます。
9
- `/topics/null/posts 404 (Not Found)`
10
- `topicId`がnullになります。
7
+ `topicId`の値を取得したいです。
11
8
 
12
- ###試したこと
13
- ・post.js
9
+ ・post.js 4~8行目
10
+ ```JavaScript
11
+ const posts = document.querySelectorAll(".post");
14
- 4行目の`const topicId = submit.getAttribute("data-id");`で`topicId`を定義します。
12
+ const topicId = posts.getAttribute("data-id");
13
+ const formData = new FormData(document.getElementById("form"));
14
+ const XHR = new XMLHttpRequest();
15
- 7行目のXHR.open("POST", `/topics/${topicId}/posts`, true);を使って`topicId`の値を取得しようとしました。
15
+ XHR.open("POST", `/topics/${topicId}/posts`, true);
16
- しかし、`/topics/null/posts 404 (Not Found)`のエラーが表示されます。
17
- `topicId`がnullになります。
16
+ ```
18
17
 
18
+ ### 発生している問題・エラーメッセージ
19
+ ・post.js 8行目
20
+ クラスのpostを取得して、そこからdata-idを取得しようと試みました。
21
+ しかし、`topicId`の値が取得できずコンソールにエラーが表示されます。
22
+ ```Console
23
+ Uncaught TypeError: post.getAttribute is not a function
24
+ ```
19
25
 
20
26
  ### 該当のソースコード
21
27
  ・post.js
@@ -23,7 +29,8 @@
23
29
  function post() {
24
30
  const submit = document.getElementById("submit");
25
31
  submit.addEventListener("click", (e) => {
32
+ const posts = document.querySelectorAll(".post");
26
- const topicId = submit.getAttribute("data-id");
33
+ const topicId = posts.getAttribute("data-id");
27
34
  const formData = new FormData(document.getElementById("form"));
28
35
  const XHR = new XMLHttpRequest();
29
36
  XHR.open("POST", `/topics/${topicId}/posts`, true);
@@ -66,7 +73,7 @@
66
73
  </div>
67
74
 
68
75
  <% @posts.each do |post| %>
69
- <div class="post">
76
+ <div class="post" data-id = <%= @topic.id %>>
70
77
  <span class="name">
71
78
  名無し
72
79
  </span>