質問編集履歴
1
コードを追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -8,40 +8,318 @@
|
|
8
8
|
|
9
9
|
|
10
10
|
|
11
|
-
###
|
11
|
+
### 該当のソースコード
|
12
|
+
|
13
|
+
|
14
|
+
|
15
|
+
controller
|
16
|
+
|
17
|
+
```Ruby
|
18
|
+
|
19
|
+
class Api::V1::PostsController < ApplicationController
|
20
|
+
|
21
|
+
def index
|
22
|
+
|
23
|
+
posts = Post.all
|
24
|
+
|
25
|
+
render json: posts
|
26
|
+
|
27
|
+
end
|
28
|
+
|
29
|
+
|
30
|
+
|
31
|
+
def create
|
32
|
+
|
33
|
+
posts = Post.new(create_params)
|
34
|
+
|
35
|
+
if posts.save
|
36
|
+
|
37
|
+
render json: posts, status: :created
|
38
|
+
|
39
|
+
else
|
40
|
+
|
41
|
+
ender json: { errors: post.errors.full_messages }, status: :internal_server_error
|
42
|
+
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
46
|
+
|
47
|
+
|
48
|
+
|
49
|
+
def show
|
50
|
+
|
51
|
+
content = post.post_items
|
52
|
+
|
53
|
+
render json: {"post": post, "content": content}, status: :ok
|
54
|
+
|
55
|
+
end
|
56
|
+
|
57
|
+
#今回対象
|
58
|
+
|
59
|
+
//////////////////////////////////////////////////////////////////////
|
60
|
+
|
61
|
+
#updateを実行すると新旧のデータが保存されてしまう
|
62
|
+
|
63
|
+
#accepts_nested_attributes_forを使用
|
64
|
+
|
65
|
+
def update
|
66
|
+
|
67
|
+
if post.update(update_params)
|
68
|
+
|
69
|
+
head :ok
|
70
|
+
|
71
|
+
else
|
72
|
+
|
73
|
+
render json: { errors: post.errors.full_messages }, status: :internal_server_error
|
74
|
+
|
75
|
+
end
|
76
|
+
|
77
|
+
end
|
78
|
+
|
79
|
+
|
80
|
+
|
81
|
+
//////////////////////////////////////////////////////////////////////
|
82
|
+
|
83
|
+
|
84
|
+
|
85
|
+
def destroy
|
86
|
+
|
87
|
+
if post.destroy
|
88
|
+
|
89
|
+
render json: {}, status: :ok
|
90
|
+
|
91
|
+
else
|
92
|
+
|
93
|
+
render json: {}, status: :internal_server_error
|
94
|
+
|
95
|
+
end
|
96
|
+
|
97
|
+
end
|
98
|
+
|
99
|
+
|
100
|
+
|
101
|
+
private
|
102
|
+
|
103
|
+
def post
|
104
|
+
|
105
|
+
@post ||= Post.find(params[:id])
|
106
|
+
|
107
|
+
end
|
108
|
+
|
109
|
+
|
110
|
+
|
111
|
+
def post_item_params
|
112
|
+
|
113
|
+
[:id, :content, :status]
|
114
|
+
|
115
|
+
end
|
116
|
+
|
117
|
+
|
118
|
+
|
119
|
+
|
120
|
+
|
121
|
+
def update_params
|
122
|
+
|
123
|
+
params.require(:post).permit(post_items_attributes: post_item_params)
|
124
|
+
|
125
|
+
end
|
126
|
+
|
127
|
+
|
128
|
+
|
129
|
+
def create_params
|
130
|
+
|
131
|
+
params.require(:post).permit(:title, :author, :image, post_items_attributes: post_item_params)
|
132
|
+
|
133
|
+
end
|
134
|
+
|
135
|
+
end
|
12
136
|
|
13
137
|
|
14
138
|
|
15
139
|
```
|
16
140
|
|
141
|
+
|
142
|
+
|
143
|
+
model
|
144
|
+
|
17
|
-
|
145
|
+
```Ruby
|
146
|
+
|
147
|
+
class Post < ApplicationRecord
|
148
|
+
|
149
|
+
has_many :post_items, dependent: :destroy
|
150
|
+
|
151
|
+
accepts_nested_attributes_for :post_items, allow_destroy: true
|
152
|
+
|
153
|
+
|
154
|
+
|
155
|
+
validates :title, presence: true
|
156
|
+
|
157
|
+
validates :author, presence: true
|
158
|
+
|
159
|
+
validates :image, presence: true
|
160
|
+
|
161
|
+
end
|
162
|
+
|
163
|
+
|
18
164
|
|
19
165
|
```
|
20
166
|
|
21
167
|
|
22
168
|
|
23
|
-
|
169
|
+
store
|
24
|
-
|
25
|
-
|
26
|
-
|
170
|
+
|
27
|
-
```
|
171
|
+
```JavaScript
|
172
|
+
|
28
|
-
|
173
|
+
import * as url from './constants/url'
|
174
|
+
|
175
|
+
|
176
|
+
|
177
|
+
export const actions = {
|
178
|
+
|
179
|
+
// ユーザーが選択した本をサーバーに送る
|
180
|
+
|
181
|
+
post (context) {
|
182
|
+
|
183
|
+
const list = context.state.todos.list
|
184
|
+
|
185
|
+
const selectedBook = context.state.book.selectedBook
|
186
|
+
|
187
|
+
|
188
|
+
|
189
|
+
// サーバーに送る配列を作成
|
190
|
+
|
191
|
+
const postItemsAttributes =
|
192
|
+
|
193
|
+
list.map((item) => {
|
194
|
+
|
195
|
+
return {
|
196
|
+
|
197
|
+
content: item.content,
|
198
|
+
|
199
|
+
status: item.status
|
200
|
+
|
201
|
+
}
|
202
|
+
|
203
|
+
})
|
204
|
+
|
205
|
+
|
206
|
+
|
207
|
+
// plugin/bookInfo $title,$author,$image
|
208
|
+
|
209
|
+
this.$axios.$post(url.POST_API + 'posts', {
|
210
|
+
|
29
|
-
|
211
|
+
post: {
|
212
|
+
|
213
|
+
title: this.$title(selectedBook),
|
214
|
+
|
215
|
+
author: this.$author(selectedBook),
|
216
|
+
|
217
|
+
image: this.$image(selectedBook),
|
218
|
+
|
219
|
+
post_items_attributes: postItemsAttributes
|
220
|
+
|
221
|
+
}
|
222
|
+
|
223
|
+
})
|
224
|
+
|
225
|
+
.then((responseBook) => {
|
226
|
+
|
227
|
+
context.commit('book/userBook', responseBook)
|
228
|
+
|
229
|
+
context.commit('book/clearBook')
|
230
|
+
|
231
|
+
context.commit('todos/clear')
|
232
|
+
|
233
|
+
})
|
234
|
+
|
235
|
+
},
|
236
|
+
|
237
|
+
get (commit) {
|
238
|
+
|
239
|
+
this.$axios.$get(url.POST_API + 'posts')
|
240
|
+
|
241
|
+
.then((response) => {
|
242
|
+
|
243
|
+
commit('registerdBook', response)
|
244
|
+
|
245
|
+
})
|
246
|
+
|
247
|
+
},
|
248
|
+
|
249
|
+
|
250
|
+
|
251
|
+
// 本を削除
|
252
|
+
|
253
|
+
delete (context) {
|
254
|
+
|
255
|
+
const bookId = context.state.book.selectedBook.id
|
256
|
+
|
257
|
+
this.$axios.$delete(url.POST_API + 'posts/' + bookId)
|
258
|
+
|
259
|
+
},
|
260
|
+
|
261
|
+
|
262
|
+
|
263
|
+
// 更新 今回対象
|
264
|
+
|
265
|
+
///////////////////////////////////////////////////////////////////////
|
266
|
+
|
267
|
+
update (context) {
|
268
|
+
|
269
|
+
|
270
|
+
|
271
|
+
// 編集データのリストを取得
|
272
|
+
|
273
|
+
const list = context.state.todos.list
|
274
|
+
|
275
|
+
|
276
|
+
|
277
|
+
//送り先ID
|
278
|
+
|
279
|
+
const selectedBook = context.state.book.selectedBook
|
280
|
+
|
281
|
+
|
282
|
+
|
283
|
+
// 編集したデータをmapで取り出し
|
284
|
+
|
285
|
+
const postItemsAttributes =
|
286
|
+
|
287
|
+
list.map((item) => {
|
288
|
+
|
289
|
+
return {
|
290
|
+
|
291
|
+
content: item.content,
|
292
|
+
|
293
|
+
status: false
|
294
|
+
|
295
|
+
}
|
296
|
+
|
297
|
+
})
|
298
|
+
|
299
|
+
this.$axios.$patch(url.POST_API + 'posts/' + selectedBook.id, {
|
300
|
+
|
301
|
+
post: {
|
302
|
+
|
303
|
+
post_items_attributes: postItemsAttributes
|
304
|
+
|
305
|
+
}
|
306
|
+
|
307
|
+
})
|
308
|
+
|
309
|
+
}
|
310
|
+
|
311
|
+
///////////////////////////////////////////////////////////////////////
|
312
|
+
|
313
|
+
|
314
|
+
|
315
|
+
}
|
316
|
+
|
317
|
+
|
30
318
|
|
31
319
|
```
|
32
320
|
|
33
|
-
|
34
|
-
|
35
|
-
### 試したこと
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
ここに問題に対して試したことを記載してください。
|
40
|
-
|
41
|
-
|
42
|
-
|
43
321
|
### 補足情報(FW/ツールのバージョンなど)
|
44
322
|
|
45
|
-
|
46
|
-
|
47
|
-
|
323
|
+
Nuxt 2.15
|
324
|
+
|
325
|
+
Rails 6
|