質問編集履歴

1

エラーのスクショの追加、ファイル名の追加

2021/04/17 10:31

投稿

ttt452
ttt452

スコア4

test CHANGED
File without changes
test CHANGED
@@ -14,47 +14,61 @@
14
14
 
15
15
 
16
16
 
17
+ ![イメージ説明](294076832e298c61c006e1f719bbcf7f.jpeg)
18
+
19
+
20
+
21
+
22
+
23
+ ### 該当のソースコード
24
+
25
+
26
+
27
+
28
+
29
+
30
+
17
- ```エラー
31
+ ```routes
18
-
32
+
33
+
34
+
19
- NoMethodError in Books#create
35
+ Rails.application.routes.draw do
36
+
20
-
37
+ # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
38
+
39
+ get '' => 'homes#top'
40
+
41
+ get 'books' => 'books#index'
42
+
43
+ post 'books' => 'books#create'
44
+
45
+ get 'books/:id' => 'books#show', as: 'book'
46
+
21
- Showing /home/ec2-user/environment/bookers/app/views/books/index.html.erb where line #23 raised:
47
+ get 'books/:id/edit' => 'books#edit', as: 'edit_book'
48
+
49
+
50
+
22
-
51
+ patch 'books/:id' => 'books#update', as: 'update_book'
23
-
24
-
52
+
25
- undefined method `each' for nil:NilClass
53
+ get 'books/:id/update' => 'books#update'
54
+
55
+ delete 'books/:id' => 'books#destroy', as: 'destroy_book'
56
+
57
+
58
+
59
+ end
26
60
 
27
61
  ```
28
62
 
29
63
 
30
64
 
31
- ### 該当のソースコード
32
-
33
- ```routes.rb
65
+ ```modelBook
66
+
34
-
67
+ class Book < ApplicationRecord
68
+
35
- Rails.application.routes.draw do
69
+ validates :title, presence: true
36
-
37
- # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
70
+
38
-
39
- get '' => 'homes#top'
71
+ validates :body, presence: true
40
-
41
- get 'books' => 'books#index'
42
-
43
- post 'books' => 'books#create'
44
-
45
- get 'books/:id' => 'books#show', as: 'book'
46
-
47
- get 'books/:id/edit' => 'books#edit', as: 'edit_book'
48
-
49
-
50
-
51
- patch 'books/:id' => 'books#update', as: 'update_book'
52
-
53
- get 'books/:id/update' => 'books#update'
54
-
55
- delete 'books/:id' => 'books#destroy', as: 'destroy_book'
56
-
57
-
58
72
 
59
73
  end
60
74
 
@@ -62,284 +76,272 @@
62
76
 
63
77
 
64
78
 
65
- ```models/book.rb
79
+ ```booksController
66
-
80
+
67
- class Book < ApplicationRecord
81
+ class BooksController < ApplicationController
82
+
83
+
84
+
68
-
85
+ def new
86
+
87
+
88
+
89
+ end
90
+
91
+
92
+
93
+ def index
94
+
95
+ @books = Book.all
96
+
97
+ @book = Book.new
98
+
99
+ end
100
+
101
+
102
+
103
+ def show
104
+
105
+ @book = Book.find(params[:id])
106
+
107
+ end
108
+
109
+
110
+
111
+ def edit
112
+
113
+ @book = Book.find(params[:id])
114
+
115
+ end
116
+
117
+
118
+
119
+
120
+
121
+
122
+
123
+ def destroy
124
+
125
+ book = Book.find(params[:id])
126
+
127
+ book.destroy
128
+
129
+ flash[:success] = "Book was successfully destroyed."
130
+
131
+ redirect_to books_path
132
+
133
+ end
134
+
135
+
136
+
137
+
138
+
139
+
140
+
141
+ def create
142
+
143
+
144
+
145
+
146
+
147
+
148
+
149
+ book = Book.new(book_params)
150
+
151
+ if book.save
152
+
153
+ flash[:success] = "Book was successfully created."
154
+
155
+ redirect_to "/books/#{book.id}"
156
+
157
+ else
158
+
159
+ # flash.now[:danger] = "Failed to save."
160
+
69
- validates :title, presence: true
161
+ #render "/books"
162
+
70
-
163
+ render :index
164
+
165
+ end
166
+
167
+ end
168
+
169
+
170
+
171
+
172
+
173
+
174
+
175
+
176
+
177
+
178
+
179
+ def update
180
+
181
+ book = Book.find(params[:id])
182
+
183
+
184
+
185
+
186
+
187
+ if book.update(book_params)
188
+
189
+ flash[:success] = "Book was successfully updated."
190
+
191
+ redirect_to "/books/#{book.id}"
192
+
193
+ else
194
+
195
+ flash.now[:danger] = "Failed to update."
196
+
197
+ render "/books"
198
+
199
+ end
200
+
201
+ end
202
+
203
+
204
+
205
+
206
+
207
+
208
+
209
+ private
210
+
211
+
212
+
213
+ def book_params
214
+
71
- validates :body, presence: true
215
+ params.require(:book).permit(:title, :body)
216
+
217
+ end
218
+
219
+
72
220
 
73
221
  end
74
222
 
223
+
224
+
75
225
  ```
76
226
 
77
227
 
78
228
 
79
- ```books_controller.rb
80
-
81
- class BooksController < ApplicationController
82
-
83
-
84
-
85
- def new
86
-
87
-
88
-
89
- end
90
-
91
-
92
-
93
- def index
94
-
95
- @books = Book.all
96
-
97
- @book = Book.new
98
-
99
- end
100
-
101
-
102
-
103
- def show
104
-
105
- @book = Book.find(params[:id])
106
-
107
- end
108
-
109
-
110
-
111
- def edit
112
-
113
- @book = Book.find(params[:id])
114
-
115
- end
116
-
117
-
118
-
119
-
120
-
121
-
122
-
123
- def destroy
124
-
125
- book = Book.find(params[:id])
126
-
127
- book.destroy
128
-
129
- flash[:success] = "Book was successfully destroyed."
130
-
131
- redirect_to books_path
132
-
133
- end
134
-
135
-
136
-
137
-
138
-
139
-
140
-
141
- def create
142
-
143
-
144
-
145
-
146
-
147
-
148
-
149
- book = Book.new(book_params)
150
-
151
- if book.save
152
-
153
- flash[:success] = "Book was successfully created."
154
-
155
- redirect_to "/books/#{book.id}"
156
-
157
- else
158
-
159
- # flash.now[:danger] = "Failed to save."
160
-
161
- #render "/books"
162
-
163
- render :index
164
-
165
- end
166
-
167
- end
168
-
169
-
170
-
171
-
172
-
173
-
174
-
175
-
176
-
177
-
178
-
179
- def update
180
-
181
- book = Book.find(params[:id])
182
-
183
-
184
-
185
-
186
-
187
- if book.update(book_params)
188
-
189
- flash[:success] = "Book was successfully updated."
190
-
191
- redirect_to "/books/#{book.id}"
192
-
193
- else
194
-
195
- flash.now[:danger] = "Failed to update."
196
-
197
- render "/books"
198
-
199
- end
200
-
201
- end
202
-
203
-
204
-
205
-
206
-
207
-
208
-
209
- private
210
-
211
-
212
-
213
- def book_params
214
-
215
- params.require(:book).permit(:title, :body)
216
-
217
- end
218
-
219
-
220
-
221
- end
229
+ ```index
230
+
231
+
232
+
233
+
234
+
235
+
236
+
237
+ <div class="notifications">
238
+
239
+ <% flash.each do |message_type, message| %>
240
+
241
+ <%= message %>
242
+
243
+ <% end %>
244
+
245
+ </div>
246
+
247
+
248
+
249
+
250
+
251
+ <h1>Books</h1>
252
+
253
+
254
+
255
+ <!-- 一覧表示 -->
256
+
257
+ <table style="table-layout: fixed;">
258
+
259
+
260
+
261
+ <tr>
262
+
263
+ <th>Title</th>
264
+
265
+ <th>Body</th>
266
+
267
+ <th colspan="3"></th>
268
+
269
+ </tr>
270
+
271
+
272
+
273
+
274
+
275
+ <% @books.each do |book| %>
276
+
277
+ <tr>
278
+
279
+ <td><%= book.title %></td>
280
+
281
+ <td><%= book.body %></td>
282
+
283
+ <!--<td><a class="show" href="/books/">Show</a></td>-->
284
+
285
+ <td><%= link_to "Show", "/books/#{book.id}" %></td>
286
+
287
+ <td><%= link_to "Edit", "/books/#{book.id}/edit" %></td>
288
+
289
+ <td><%= link_to "Destroy", destroy_book_path(book.id), method: :delete, "data-confirm" => "Are you sure?" %></td>
290
+
291
+ </tr>
292
+
293
+ <% end %>
294
+
295
+ </table>
296
+
297
+
298
+
299
+
300
+
301
+ <% if @book.errors.any? %>
302
+
303
+ <%= @book.errors.count %>件のエラーが発生しました
304
+
305
+ <% @book.errors.full_messages.each do |message| %>
306
+
307
+ <%= message %>
308
+
309
+ <% end %>
310
+
311
+ <% end %>
312
+
313
+
314
+
315
+ <!-- 新規投稿 -->
316
+
317
+ <%= form_with model:@book, url:'/books', local:true do |f| %>
318
+
319
+ <div class="new-book">
320
+
321
+ <h2>New book</h2>
322
+
323
+ <p>Title</p>
324
+
325
+ <%= f.text_field :title %>
326
+
327
+ <p>Body</p>
328
+
329
+ <%= f.text_area :body %>
330
+
331
+ <div>
332
+
333
+ <%= f.submit 'Create Book' %>
334
+
335
+ </div>
336
+
337
+ </div>
338
+
339
+ <% end %>
222
340
 
223
341
 
224
342
 
225
343
  ```
226
344
 
227
- ```index.html.erb
228
-
229
-
230
-
231
-
232
-
233
-
234
-
235
- <div class="notifications">
236
-
237
- <% flash.each do |message_type, message| %>
238
-
239
- <%= message %>
240
-
241
- <% end %>
242
-
243
- </div>
244
-
245
-
246
-
247
-
248
-
249
- <h1>Books</h1>
250
-
251
-
252
-
253
- <!-- 一覧表示 -->
254
-
255
- <table style="table-layout: fixed;">
256
-
257
-
258
-
259
- <tr>
260
-
261
- <th>Title</th>
262
-
263
- <th>Body</th>
264
-
265
- <th colspan="3"></th>
266
-
267
- </tr>
268
-
269
-
270
-
271
-
272
-
273
- <% @books.each do |book| %>
274
-
275
- <tr>
276
-
277
- <td><%= book.title %></td>
278
-
279
- <td><%= book.body %></td>
280
-
281
- <!--<td><a class="show" href="/books/">Show</a></td>-->
282
-
283
- <td><%= link_to "Show", "/books/#{book.id}" %></td>
284
-
285
- <td><%= link_to "Edit", "/books/#{book.id}/edit" %></td>
286
-
287
- <td><%= link_to "Destroy", destroy_book_path(book.id), method: :delete, "data-confirm" => "Are you sure?" %></td>
288
-
289
- </tr>
290
-
291
- <% end %>
292
-
293
- </table>
294
-
295
-
296
-
297
-
298
-
299
- <% if @book.errors.any? %>
300
-
301
- <%= @book.errors.count %>件のエラーが発生しました
302
-
303
- <% @book.errors.full_messages.each do |message| %>
304
-
305
- <%= message %>
306
-
307
- <% end %>
308
-
309
- <% end %>
310
-
311
-
312
-
313
- <!-- 新規投稿 -->
314
-
315
- <%= form_with model:@book, url:'/books', local:true do |f| %>
316
-
317
- <div class="new-book">
318
-
319
- <h2>New book</h2>
320
-
321
- <p>Title</p>
322
-
323
- <%= f.text_field :title %>
324
-
325
- <p>Body</p>
326
-
327
- <%= f.text_area :body %>
328
-
329
- <div>
330
-
331
- <%= f.submit 'Create Book' %>
332
-
333
- </div>
334
-
335
- </div>
336
-
337
- <% end %>
338
-
339
-
340
-
341
- ```
342
-
343
345
 
344
346
 
345
347
  ### 試したこと