質問編集履歴
17
追記
test
CHANGED
File without changes
|
test
CHANGED
@@ -7,7 +7,7 @@
|
|
7
7
|
|
8
8
|
```
|
9
9
|
[Rails+Vue.js]~Bookshelf~だけ表示される。
|
10
|
-
|
10
|
+
データが表示されない
|
11
11
|
```
|
12
12
|
|
13
13
|
### 該当のソースコード
|
16
追記
test
CHANGED
File without changes
|
test
CHANGED
@@ -7,36 +7,6 @@
|
|
7
7
|
|
8
8
|
```
|
9
9
|
[Rails+Vue.js]~Bookshelf~だけ表示される。
|
10
|
-
|
11
|
-
|
12
|
-
localhost:5173では
|
13
|
-
Failed to load resource: the server responded with a status of 404 (Not Found)
|
14
|
-
|
15
|
-
AxiosError {message: 'Request failed with status code 404', name: 'AxiosError', code: 'ERR_BAD_REQUEST', config: {…}, request: XMLHttpRequest, …}
|
16
|
-
code
|
17
|
-
:
|
18
|
-
"ERR_BAD_REQUEST"
|
19
|
-
config
|
20
|
-
:
|
21
|
-
{transitional: {…}, adapter: Array(2), transformRequest: Array(1), transformResponse: Array(1), timeout: 0, …}
|
22
|
-
message
|
23
|
-
:
|
24
|
-
"Request failed with status code 404"
|
25
|
-
name
|
26
|
-
:
|
27
|
-
"AxiosError"
|
28
|
-
request
|
29
|
-
:
|
30
|
-
XMLHttpRequest {onreadystatechange: null, readyState: 4, timeout: 0, withCredentials: false, upload: XMLHttpRequestUpload, …}
|
31
|
-
response
|
32
|
-
:
|
33
|
-
{data: '', status: 404, statusText: 'Not Found', headers: AxiosHeaders, config: {…}, …}
|
34
|
-
stack
|
35
|
-
:
|
36
|
-
"AxiosError: Request failed with status code 404\n at settle (http://localhost:5173/node_modules/.vite/deps/axios.js?v=f429fad1:1187:12)\n at XMLHttpRequest.onloadend (http://localhost:5173/node_modules/.vite/deps/axios.js?v=f429fad1:1415:7)"
|
37
|
-
[[Prototype]]
|
38
|
-
:
|
39
|
-
Error
|
40
10
|
|
41
11
|
```
|
42
12
|
|
@@ -114,16 +84,18 @@
|
|
114
84
|
```app/controllers/api/books_controller.rb
|
115
85
|
|
116
86
|
class Api::BooksController < ApplicationController
|
117
|
-
|
118
87
|
def index
|
119
|
-
|
88
|
+
books = Book.all
|
89
|
+
|
90
|
+
render json: books
|
91
|
+
|
120
|
-
|
92
|
+
# 質問主さんの書き方の場合、フロント側はbooks.value = res.data.bookで受け取る
|
93
|
+
# render json: {book: books}
|
121
94
|
end
|
122
95
|
|
123
96
|
def show
|
124
|
-
|
97
|
+
book = Book.find(params[:id])
|
125
|
-
|
98
|
+
render json: book
|
126
|
-
render 'show', formats: 'json', handlers: 'jbuilder'
|
127
99
|
end
|
128
100
|
end
|
129
101
|
|
@@ -132,8 +104,7 @@
|
|
132
104
|
```cors.rb
|
133
105
|
Rails.application.config.middleware.insert_before 0, Rack::Cors do
|
134
106
|
allow do
|
135
|
-
origins "
|
107
|
+
origins "localhost:5173"
|
136
|
-
|
137
108
|
resource "*",
|
138
109
|
headers: :any,
|
139
110
|
methods: [:get, :post, :put, :patch, :delete, :options, :head]
|
@@ -145,6 +116,7 @@
|
|
145
116
|
```frontend/vite-project/src/lib/axios.ts
|
146
117
|
|
147
118
|
import axios from 'axios'
|
119
|
+
|
148
120
|
export default axios.create({
|
149
121
|
baseURL: 'http://localhost:3000',
|
150
122
|
})
|
@@ -268,38 +240,10 @@
|
|
268
240
|
ここの部分の情報はありますか?
|
269
241
|
とかありましたら記載します。
|
270
242
|
|
271
|
-
|
243
|
+
|
272
|
-
|
244
|
+
|
273
|
-
|
274
|
-
|
245
|
+
|
275
|
-
|
276
|
-
|
246
|
+
|
277
|
-
export default axios.create({
|
278
|
-
baseURL: 'http://localhost:3000',
|
279
|
-
})
|
280
|
-
```
|
281
|
-
に修正しました。
|
282
|
-
|
283
|
-
```
|
284
|
-
docker compose run --rm backend bundle exec rails c
|
285
|
-
pry> 20.times do
|
286
|
-
Book.create(
|
287
|
-
title: Faker::Book.title,
|
288
|
-
author: Faker::Book.author,
|
289
|
-
publisher: Faker::Book.publisher,
|
290
|
-
genre: Faker::Book.genre
|
291
|
-
)
|
292
|
-
end
|
293
|
-
|
294
|
-
データの確認
|
295
|
-
pry> Book.all
|
296
|
-
```
|
297
|
-
を行いました
|
298
|
-
|
299
|
-
やったコマンド
|
300
|
-
```
|
301
|
-
docker-compose run backend rails db:seed
|
302
|
-
docker-compose run backend rails db:migrate
|
303
247
|
```
|
304
248
|
環境は
|
305
249
|
https://qiita.com/gabakugik/items/cded7e8aaa014025d8ba
|
15
追記
test
CHANGED
File without changes
|
test
CHANGED
@@ -294,6 +294,7 @@
|
|
294
294
|
データの確認
|
295
295
|
pry> Book.all
|
296
296
|
```
|
297
|
+
を行いました
|
297
298
|
|
298
299
|
やったコマンド
|
299
300
|
```
|
14
修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
### 実現したいこと
|
2
2
|
APIでたたいたのをVueで表示させたい。
|
3
|
-
|
3
|
+
|
4
4
|
|
5
5
|
|
6
6
|
### 発生している問題・エラーメッセージ
|
@@ -8,16 +8,36 @@
|
|
8
8
|
```
|
9
9
|
[Rails+Vue.js]~Bookshelf~だけ表示される。
|
10
10
|
|
11
|
-
http://localhost:3000/api/books
|
12
|
-
を行うと
|
13
|
-
No route matches [GET] "/api/books"
|
14
|
-
になります
|
15
|
-
ルーティングエラーになる。
|
16
|
-
jsonのデータが取れてない。
|
17
11
|
|
18
12
|
localhost:5173では
|
19
13
|
Failed to load resource: the server responded with a status of 404 (Not Found)
|
20
|
-
|
14
|
+
|
15
|
+
AxiosError {message: 'Request failed with status code 404', name: 'AxiosError', code: 'ERR_BAD_REQUEST', config: {…}, request: XMLHttpRequest, …}
|
16
|
+
code
|
17
|
+
:
|
18
|
+
"ERR_BAD_REQUEST"
|
19
|
+
config
|
20
|
+
:
|
21
|
+
{transitional: {…}, adapter: Array(2), transformRequest: Array(1), transformResponse: Array(1), timeout: 0, …}
|
22
|
+
message
|
23
|
+
:
|
24
|
+
"Request failed with status code 404"
|
25
|
+
name
|
26
|
+
:
|
27
|
+
"AxiosError"
|
28
|
+
request
|
29
|
+
:
|
30
|
+
XMLHttpRequest {onreadystatechange: null, readyState: 4, timeout: 0, withCredentials: false, upload: XMLHttpRequestUpload, …}
|
31
|
+
response
|
32
|
+
:
|
33
|
+
{data: '', status: 404, statusText: 'Not Found', headers: AxiosHeaders, config: {…}, …}
|
34
|
+
stack
|
35
|
+
:
|
36
|
+
"AxiosError: Request failed with status code 404\n at settle (http://localhost:5173/node_modules/.vite/deps/axios.js?v=f429fad1:1187:12)\n at XMLHttpRequest.onloadend (http://localhost:5173/node_modules/.vite/deps/axios.js?v=f429fad1:1415:7)"
|
37
|
+
[[Prototype]]
|
38
|
+
:
|
39
|
+
Error
|
40
|
+
|
21
41
|
```
|
22
42
|
|
23
43
|
### 該当のソースコード
|
@@ -86,18 +106,27 @@
|
|
86
106
|
```config/routes.rb
|
87
107
|
Rails.application.routes.draw do
|
88
108
|
namespace :api do
|
89
|
-
resources :books
|
109
|
+
resources :books
|
90
110
|
end
|
91
111
|
end
|
92
112
|
```
|
93
113
|
|
94
114
|
```app/controllers/api/books_controller.rb
|
115
|
+
|
95
116
|
class Api::BooksController < ApplicationController
|
117
|
+
|
118
|
+
def index
|
119
|
+
@book = Book.all
|
120
|
+
render 'all', formats: 'json', handlers: 'jbuilder'
|
121
|
+
end
|
122
|
+
|
96
123
|
def show
|
97
124
|
@book = Book.find(params[:id])
|
125
|
+
# render status: 200, json: {book: @book}
|
98
|
-
render 'show', formats: 'json', handlers: 'jbuilder'
|
126
|
+
render 'show', formats: 'json', handlers: 'jbuilder'
|
99
127
|
end
|
100
128
|
end
|
129
|
+
|
101
130
|
```
|
102
131
|
|
103
132
|
```cors.rb
|
@@ -120,6 +149,18 @@
|
|
120
149
|
baseURL: 'http://localhost:3000',
|
121
150
|
})
|
122
151
|
```
|
152
|
+
|
153
|
+
```main.ts
|
154
|
+
import { createApp } from 'vue'
|
155
|
+
import './style.css'
|
156
|
+
import App from './App.vue'
|
157
|
+
import router from './router'
|
158
|
+
|
159
|
+
createApp(App).use(router).mount('#app')
|
160
|
+
```
|
161
|
+
|
162
|
+
|
163
|
+
|
123
164
|
|
124
165
|
```package.json
|
125
166
|
{
|
@@ -205,7 +246,7 @@
|
|
205
246
|
};
|
206
247
|
|
207
248
|
const setBookInfo = (id: number) => {
|
208
|
-
axios.get(`api/books/${id}.json`).then((res) => {
|
249
|
+
axios.get(`/api/books/${id}.json`).then((res) => {
|
209
250
|
bookInfo.value = res.data;
|
210
251
|
bookInfoBool.value = true;
|
211
252
|
});
|
@@ -239,6 +280,21 @@
|
|
239
280
|
```
|
240
281
|
に修正しました。
|
241
282
|
|
283
|
+
```
|
284
|
+
docker compose run --rm backend bundle exec rails c
|
285
|
+
pry> 20.times do
|
286
|
+
Book.create(
|
287
|
+
title: Faker::Book.title,
|
288
|
+
author: Faker::Book.author,
|
289
|
+
publisher: Faker::Book.publisher,
|
290
|
+
genre: Faker::Book.genre
|
291
|
+
)
|
292
|
+
end
|
293
|
+
|
294
|
+
データの確認
|
295
|
+
pry> Book.all
|
296
|
+
```
|
297
|
+
|
242
298
|
やったコマンド
|
243
299
|
```
|
244
300
|
docker-compose run backend rails db:seed
|
13
追記
test
CHANGED
File without changes
|
test
CHANGED
@@ -15,6 +15,9 @@
|
|
15
15
|
ルーティングエラーになる。
|
16
16
|
jsonのデータが取れてない。
|
17
17
|
|
18
|
+
localhost:5173では
|
19
|
+
Failed to load resource: the server responded with a status of 404 (Not Found)
|
20
|
+
になる
|
18
21
|
```
|
19
22
|
|
20
23
|
### 該当のソースコード
|
12
更新
test
CHANGED
File without changes
|
test
CHANGED
@@ -7,15 +7,6 @@
|
|
7
7
|
|
8
8
|
```
|
9
9
|
[Rails+Vue.js]~Bookshelf~だけ表示される。
|
10
|
-
|
11
|
-
irb(main):005:0> Book.find(1)
|
12
|
-
Book Load (0.5ms) SELECT `books`.* FROM `books` WHERE `books`.`id` = 1 LIMIT 1
|
13
|
-
/usr/local/bundle/gems/activerecord-7.0.7/lib/active_record/core.rb:284:in `find': Couldn't find Book with 'id'=1 (ActiveRecord::RecordNotFound)
|
14
|
-
|
15
|
-
irb(main):007:0> Book.all
|
16
|
-
Book Load (0.4ms) SELECT `books`.* FROM `books`
|
17
|
-
=> []
|
18
|
-
なんでデータがないんでしょうか?
|
19
10
|
|
20
11
|
http://localhost:3000/api/books
|
21
12
|
を行うと
|
11
追記
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
### 実現したいこと
|
2
2
|
APIでたたいたのをVueで表示させたい。
|
3
|
-
|
3
|
+
JSONのデータをブラウザ上で表示させたい。
|
4
4
|
|
5
5
|
|
6
6
|
### 発生している問題・エラーメッセージ
|
10
追記
test
CHANGED
File without changes
|
test
CHANGED
@@ -22,7 +22,7 @@
|
|
22
22
|
No route matches [GET] "/api/books"
|
23
23
|
になります
|
24
24
|
ルーティングエラーになる。
|
25
|
-
jsonのデータが取れない。
|
25
|
+
jsonのデータが取れてない。
|
26
26
|
|
27
27
|
```
|
28
28
|
|
9
追記
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
Vueでrails apiのデータを表示させたい
|
1
|
+
Vueでrails apiのデータを表示させたい
|
test
CHANGED
@@ -17,41 +17,12 @@
|
|
17
17
|
=> []
|
18
18
|
なんでデータがないんでしょうか?
|
19
19
|
|
20
|
-
frontend上で開発者モード
|
21
|
-
|
22
|
-
App.vue:45
|
23
|
-
GET http://localhost:5173/api/books 404 (Not Found)
|
24
|
-
App.vue:50
|
25
|
-
AxiosError {message: 'Request failed with status code 404', name: 'AxiosError', code: 'ERR_BAD_REQUEST', config: {…}, request: XMLHttpRequest, …}
|
26
|
-
code
|
27
|
-
:
|
28
|
-
"ERR_BAD_REQUEST"
|
29
|
-
config
|
30
|
-
:
|
31
|
-
{transitional: {…}, adapter: Array(2), transformRequest: Array(1), transformResponse: Array(1), timeout: 0, …}
|
32
|
-
message
|
33
|
-
:
|
34
|
-
"Request failed with status code 404"
|
35
|
-
name
|
36
|
-
:
|
37
|
-
"AxiosError"
|
38
|
-
request
|
39
|
-
:
|
40
|
-
XMLHttpRequest {onreadystatechange: null, readyState: 4, timeout: 0, withCredentials: false, upload: XMLHttpRequestUpload, …}
|
41
|
-
response
|
42
|
-
:
|
43
|
-
{data: '', status: 404, statusText: 'Not Found', headers: AxiosHeaders, config: {…}, …}
|
44
|
-
stack
|
45
|
-
:
|
46
|
-
"AxiosError: Request failed with status code 404\n at settle (http://localhost:5173/node_modules/.vite/deps/axios.js?v=f429fad1:1187:12)\n at XMLHttpRequest.onloadend (http://localhost:5173/node_modules/.vite/deps/axios.js?v=f429fad1:1415:7)"
|
47
|
-
[[Prototype]]
|
48
|
-
:
|
49
|
-
Error
|
50
|
-
|
51
20
|
http://localhost:3000/api/books
|
52
21
|
を行うと
|
53
22
|
No route matches [GET] "/api/books"
|
54
23
|
になります
|
24
|
+
ルーティングエラーになる。
|
25
|
+
jsonのデータが取れない。
|
55
26
|
|
56
27
|
```
|
57
28
|
|
8
追記
test
CHANGED
File without changes
|
test
CHANGED
@@ -47,6 +47,12 @@
|
|
47
47
|
[[Prototype]]
|
48
48
|
:
|
49
49
|
Error
|
50
|
+
|
51
|
+
http://localhost:3000/api/books
|
52
|
+
を行うと
|
53
|
+
No route matches [GET] "/api/books"
|
54
|
+
になります
|
55
|
+
|
50
56
|
```
|
51
57
|
|
52
58
|
### 該当のソースコード
|
7
追記
test
CHANGED
File without changes
|
test
CHANGED
@@ -18,7 +18,10 @@
|
|
18
18
|
なんでデータがないんでしょうか?
|
19
19
|
|
20
20
|
frontend上で開発者モード
|
21
|
+
|
22
|
+
App.vue:45
|
21
|
-
|
23
|
+
GET http://localhost:5173/api/books 404 (Not Found)
|
24
|
+
App.vue:50
|
22
25
|
AxiosError {message: 'Request failed with status code 404', name: 'AxiosError', code: 'ERR_BAD_REQUEST', config: {…}, request: XMLHttpRequest, …}
|
23
26
|
code
|
24
27
|
:
|
@@ -44,7 +47,6 @@
|
|
44
47
|
[[Prototype]]
|
45
48
|
:
|
46
49
|
Error
|
47
|
-
が出てました
|
48
50
|
```
|
49
51
|
|
50
52
|
### 該当のソースコード
|
6
追記
test
CHANGED
File without changes
|
test
CHANGED
@@ -140,14 +140,14 @@
|
|
140
140
|
|
141
141
|
```
|
142
142
|
|
143
|
-
```frontend/src/lib/axios.ts
|
143
|
+
```frontend/vite-project/src/lib/axios.ts
|
144
144
|
|
145
145
|
import axios from 'axios'
|
146
146
|
export default axios.create({
|
147
|
-
baseURL: 'http://localhost:3000
|
147
|
+
baseURL: 'http://localhost:3000',
|
148
148
|
})
|
149
|
-
|
150
|
-
```
|
149
|
+
```
|
150
|
+
|
151
151
|
```package.json
|
152
152
|
{
|
153
153
|
"name": "vite-project",
|
@@ -257,6 +257,15 @@
|
|
257
257
|
アドバイスをもらい、一件rails cで入力してデータベースまでは反映されましたが
|
258
258
|
web上には表示されないです。
|
259
259
|
|
260
|
+
```frontend/vite-project/src/lib/axios.ts
|
261
|
+
|
262
|
+
import axios from 'axios'
|
263
|
+
export default axios.create({
|
264
|
+
baseURL: 'http://localhost:3000',
|
265
|
+
})
|
266
|
+
```
|
267
|
+
に修正しました。
|
268
|
+
|
260
269
|
やったコマンド
|
261
270
|
```
|
262
271
|
docker-compose run backend rails db:seed
|
5
追記
test
CHANGED
File without changes
|
test
CHANGED
@@ -19,7 +19,31 @@
|
|
19
19
|
|
20
20
|
frontend上で開発者モード
|
21
21
|
Failed to load resource: the server responded with a status of 404 (Not Found)
|
22
|
-
|
22
|
+
AxiosError {message: 'Request failed with status code 404', name: 'AxiosError', code: 'ERR_BAD_REQUEST', config: {…}, request: XMLHttpRequest, …}
|
23
|
+
code
|
24
|
+
:
|
25
|
+
"ERR_BAD_REQUEST"
|
26
|
+
config
|
27
|
+
:
|
28
|
+
{transitional: {…}, adapter: Array(2), transformRequest: Array(1), transformResponse: Array(1), timeout: 0, …}
|
29
|
+
message
|
30
|
+
:
|
31
|
+
"Request failed with status code 404"
|
32
|
+
name
|
33
|
+
:
|
34
|
+
"AxiosError"
|
35
|
+
request
|
36
|
+
:
|
37
|
+
XMLHttpRequest {onreadystatechange: null, readyState: 4, timeout: 0, withCredentials: false, upload: XMLHttpRequestUpload, …}
|
38
|
+
response
|
39
|
+
:
|
40
|
+
{data: '', status: 404, statusText: 'Not Found', headers: AxiosHeaders, config: {…}, …}
|
41
|
+
stack
|
42
|
+
:
|
43
|
+
"AxiosError: Request failed with status code 404\n at settle (http://localhost:5173/node_modules/.vite/deps/axios.js?v=f429fad1:1187:12)\n at XMLHttpRequest.onloadend (http://localhost:5173/node_modules/.vite/deps/axios.js?v=f429fad1:1415:7)"
|
44
|
+
[[Prototype]]
|
45
|
+
:
|
46
|
+
Error
|
23
47
|
が出てました
|
24
48
|
```
|
25
49
|
|
4
追記
test
CHANGED
File without changes
|
test
CHANGED
@@ -16,6 +16,11 @@
|
|
16
16
|
Book Load (0.4ms) SELECT `books`.* FROM `books`
|
17
17
|
=> []
|
18
18
|
なんでデータがないんでしょうか?
|
19
|
+
|
20
|
+
frontend上で開発者モード
|
21
|
+
Failed to load resource: the server responded with a status of 404 (Not Found)
|
22
|
+
|
23
|
+
が出てました
|
19
24
|
```
|
20
25
|
|
21
26
|
### 該当のソースコード
|
3
追記
test
CHANGED
File without changes
|
test
CHANGED
@@ -225,6 +225,9 @@
|
|
225
225
|
ここの部分の情報はありますか?
|
226
226
|
とかありましたら記載します。
|
227
227
|
|
228
|
+
アドバイスをもらい、一件rails cで入力してデータベースまでは反映されましたが
|
229
|
+
web上には表示されないです。
|
230
|
+
|
228
231
|
やったコマンド
|
229
232
|
```
|
230
233
|
docker-compose run backend rails db:seed
|
2
追記
test
CHANGED
File without changes
|
test
CHANGED
@@ -119,7 +119,7 @@
|
|
119
119
|
})
|
120
120
|
|
121
121
|
```
|
122
|
-
```
|
122
|
+
```package.json
|
123
123
|
{
|
124
124
|
"name": "vite-project",
|
125
125
|
"private": true,
|
1
追記
test
CHANGED
File without changes
|
test
CHANGED
@@ -19,7 +19,7 @@
|
|
19
19
|
```
|
20
20
|
|
21
21
|
### 該当のソースコード
|
22
|
-
|
22
|
+
やったこと
|
23
23
|
```
|
24
24
|
階層
|
25
25
|
├── backend
|