質問編集履歴
1
書式の改善
test
CHANGED
File without changes
|
test
CHANGED
@@ -14,39 +14,33 @@
|
|
14
14
|
|
15
15
|
### 発生している問題・エラーメッセージ
|
16
16
|
|
17
|
+
```
|
18
|
+
|
17
|
-
N
|
19
|
+
NoMethodError in Posts#index
|
18
20
|
|
19
21
|
Showing /vagrant/new_app/app/views/posts/index.html.erb where line #6 raised:
|
20
22
|
|
21
23
|
|
22
24
|
|
23
|
-
undefined
|
25
|
+
undefined method `image_name' for nil:NilClass
|
24
26
|
|
25
27
|
Extracted source (around line #6):
|
26
28
|
|
29
|
+
|
30
|
+
|
27
|
-
4
|
31
|
+
4 <div class="posts-index-item">
|
28
|
-
|
32
|
+
|
29
|
-
5
|
33
|
+
5 <div class="post-left">
|
30
|
-
|
34
|
+
|
31
|
-
6
|
35
|
+
6 <img src="<%= "/post.user_images/#{post.user.image_name}" %>">
|
32
|
-
|
36
|
+
|
33
|
-
7
|
37
|
+
7 </div>
|
34
|
-
|
38
|
+
|
35
|
-
8
|
39
|
+
8 <div class="post-right">
|
36
|
-
|
40
|
+
|
37
|
-
9
|
41
|
+
9 <div class="post-user-name">
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
42
|
+
|
43
|
+
|
50
44
|
|
51
45
|
Rails.root: /vagrant/new_app
|
52
46
|
|
@@ -54,9 +48,9 @@
|
|
54
48
|
|
55
49
|
Application Trace | Framework Trace | Full Trace
|
56
50
|
|
57
|
-
app/views/posts/index.html.erb:6:in `block in _app_views_posts_index_html_erb__3
|
51
|
+
app/views/posts/index.html.erb:6:in `block in _app_views_posts_index_html_erb___226630752__647422898'
|
58
|
-
|
52
|
+
|
59
|
-
app/views/posts/index.html.erb:3:in `_app_views_posts_index_html_erb__3
|
53
|
+
app/views/posts/index.html.erb:3:in `_app_views_posts_index_html_erb___226630752__647422898'
|
60
54
|
|
61
55
|
Request
|
62
56
|
|
@@ -82,11 +76,11 @@
|
|
82
76
|
|
83
77
|
None
|
84
78
|
|
85
|
-
|
79
|
+
```
|
86
|
-
|
87
|
-
|
88
|
-
|
80
|
+
|
81
|
+
|
82
|
+
|
89
|
-
### 該当のソースコード
|
83
|
+
### 該当のソースコード(routes.rb)
|
90
84
|
|
91
85
|
|
92
86
|
|
@@ -140,12 +134,240 @@
|
|
140
134
|
|
141
135
|
```
|
142
136
|
|
137
|
+
|
138
|
+
|
143
|
-
### 該当のソースコード
|
139
|
+
### 該当のソースコード(posts_controller.rb)
|
144
140
|
|
145
141
|
|
146
142
|
|
147
143
|
```Ruby
|
148
144
|
|
145
|
+
class PostsController < ApplicationController
|
146
|
+
|
147
|
+
|
148
|
+
|
149
|
+
before_action :authenticate_user
|
150
|
+
|
151
|
+
|
152
|
+
|
153
|
+
def index
|
154
|
+
|
155
|
+
@posts = Post.all.order(created_at: :desc)
|
156
|
+
|
157
|
+
end
|
158
|
+
|
159
|
+
|
160
|
+
|
161
|
+
def show
|
162
|
+
|
163
|
+
@post = Post.find_by(id: params[:id])
|
164
|
+
|
165
|
+
@user = @post.user
|
166
|
+
|
167
|
+
end
|
168
|
+
|
169
|
+
|
170
|
+
|
171
|
+
def new
|
172
|
+
|
173
|
+
@post = Post.new
|
174
|
+
|
175
|
+
end
|
176
|
+
|
177
|
+
|
178
|
+
|
179
|
+
def create
|
180
|
+
|
181
|
+
@post = Post.new(
|
182
|
+
|
183
|
+
content: params[:content],
|
184
|
+
|
185
|
+
user_id: @current_user.id
|
186
|
+
|
187
|
+
)
|
188
|
+
|
189
|
+
if @post.save
|
190
|
+
|
191
|
+
flash[:notice] ="投稿を作成しました"
|
192
|
+
|
193
|
+
redirect_to("/posts/index")
|
194
|
+
|
195
|
+
else
|
196
|
+
|
197
|
+
render("/posts/new")
|
198
|
+
|
199
|
+
end
|
200
|
+
|
201
|
+
end
|
202
|
+
|
203
|
+
|
204
|
+
|
205
|
+
def edit
|
206
|
+
|
207
|
+
@post = Post.find_by(id: params[:id])
|
208
|
+
|
209
|
+
end
|
210
|
+
|
211
|
+
|
212
|
+
|
213
|
+
def update
|
214
|
+
|
215
|
+
@post = Post.find_by(id: params[:id])
|
216
|
+
|
217
|
+
@post.content = params[:content]
|
218
|
+
|
219
|
+
if @post.save
|
220
|
+
|
221
|
+
flash[:notice] = "投稿を編集しました"
|
222
|
+
|
223
|
+
redirect_to("/posts/index")
|
224
|
+
|
225
|
+
else
|
226
|
+
|
227
|
+
render("/posts/edit")
|
228
|
+
|
229
|
+
end
|
230
|
+
|
231
|
+
end
|
232
|
+
|
233
|
+
|
234
|
+
|
235
|
+
def destroy
|
236
|
+
|
237
|
+
@post = Post.find_by(id: params[:id])
|
238
|
+
|
239
|
+
@post.destroy
|
240
|
+
|
241
|
+
flash[:notice] ="投稿を削除しました"
|
242
|
+
|
243
|
+
redirect_to("/posts/index")
|
244
|
+
|
245
|
+
end
|
246
|
+
|
247
|
+
|
248
|
+
|
249
|
+
end
|
250
|
+
|
251
|
+
```
|
252
|
+
|
253
|
+
|
254
|
+
|
255
|
+
### 該当のソースコード(index.html.erb)
|
256
|
+
|
257
|
+
|
258
|
+
|
259
|
+
```HTML
|
260
|
+
|
261
|
+
<div class="main posts-index">
|
262
|
+
|
263
|
+
<div class="container">
|
264
|
+
|
265
|
+
<% @posts.each do |post| %>
|
266
|
+
|
267
|
+
<div class="posts-index-item">
|
268
|
+
|
269
|
+
<div class="post-left">
|
270
|
+
|
271
|
+
<img src="<%= "/post.user_images/#{post.user.image_name}" %>">
|
272
|
+
|
273
|
+
</div>
|
274
|
+
|
275
|
+
<div class="post-right">
|
276
|
+
|
277
|
+
<div class="post-user-name">
|
278
|
+
|
279
|
+
<%= link_to(post.user.name, "/users/#{post.user.id}") %>
|
280
|
+
|
281
|
+
</div>
|
282
|
+
|
283
|
+
<%= link_to(post.content, "/posts/#{post.id}") %>
|
284
|
+
|
285
|
+
</div>
|
286
|
+
|
287
|
+
</div>
|
288
|
+
|
289
|
+
<% end %>
|
290
|
+
|
291
|
+
</div>
|
292
|
+
|
293
|
+
</div>
|
294
|
+
|
295
|
+
```
|
296
|
+
|
297
|
+
|
298
|
+
|
299
|
+
### 該当のソースコード(show.html.erb)
|
300
|
+
|
301
|
+
```HTML
|
302
|
+
|
303
|
+
<div class="main posts-show">
|
304
|
+
|
305
|
+
<div class="container">
|
306
|
+
|
307
|
+
<div class="posts-show-item">
|
308
|
+
|
309
|
+
<div class="post-user-name">
|
310
|
+
|
311
|
+
<img src="<%= "/user_images/#{@user.image_name}" %>">
|
312
|
+
|
313
|
+
<%= link_to(@user.name, "/users/#{@user.id}") %>
|
314
|
+
|
315
|
+
</div>
|
316
|
+
|
317
|
+
<p>
|
318
|
+
|
319
|
+
<%= @post.content %>
|
320
|
+
|
321
|
+
</p>
|
322
|
+
|
323
|
+
<div class="post-time">
|
324
|
+
|
325
|
+
<%= @post.created_at %>
|
326
|
+
|
327
|
+
</div>
|
328
|
+
|
329
|
+
<div class="post-menus">
|
330
|
+
|
331
|
+
<%= link_to("編集", "/posts/#{@post.id}/edit") %>
|
332
|
+
|
333
|
+
<%= link_to("削除", "/posts/#{@post.id}/destroy",{method: "post"}) %>
|
334
|
+
|
335
|
+
</div>
|
336
|
+
|
337
|
+
</div>
|
338
|
+
|
339
|
+
</div>
|
340
|
+
|
341
|
+
</div>
|
342
|
+
|
343
|
+
```
|
344
|
+
|
345
|
+
|
346
|
+
|
347
|
+
### 該当のソースコード(user.rb)
|
348
|
+
|
349
|
+
```Ruby
|
350
|
+
|
351
|
+
class User < ApplicationRecord
|
352
|
+
|
353
|
+
validates :name,{presence: true}
|
354
|
+
|
355
|
+
validates :email, {presence: true, uniqueness: true}
|
356
|
+
|
357
|
+
validates :password, {presence: true}
|
358
|
+
|
359
|
+
end
|
360
|
+
|
361
|
+
```
|
362
|
+
|
363
|
+
|
364
|
+
|
365
|
+
|
366
|
+
|
367
|
+
### 該当のソースコード(post.rb)
|
368
|
+
|
369
|
+
```Ruby
|
370
|
+
|
149
371
|
class Post < ApplicationRecord
|
150
372
|
|
151
373
|
validates :content, {presence: true, length: {maximum: 140}}
|
@@ -166,168 +388,8 @@
|
|
166
388
|
|
167
389
|
```
|
168
390
|
|
169
|
-
### 該当のソースコード
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
```Ruby
|
174
|
-
|
175
|
-
class PostsController < ApplicationController
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
before_action :authenticate_user
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
def index
|
184
|
-
|
185
|
-
@posts = Post.all.order(created_at: :desc)
|
186
|
-
|
187
|
-
end
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
def show
|
192
|
-
|
193
|
-
@post = Post.find_by(id: params[:id])
|
194
|
-
|
195
|
-
@user = @post.user
|
196
|
-
|
197
|
-
end
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
def new
|
202
|
-
|
203
|
-
@post = Post.new
|
204
|
-
|
205
|
-
end
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
def create
|
210
|
-
|
211
|
-
@post = Post.new(
|
212
|
-
|
213
|
-
content: params[:content],
|
214
|
-
|
215
|
-
user_id: @current_user.id
|
216
|
-
|
217
|
-
)
|
218
|
-
|
219
|
-
if @post.save
|
220
|
-
|
221
|
-
flash[:notice] ="投稿を作成しました"
|
222
|
-
|
223
|
-
redirect_to("/posts/index")
|
224
|
-
|
225
|
-
else
|
226
|
-
|
227
|
-
render("/posts/new")
|
228
|
-
|
229
|
-
end
|
230
|
-
|
231
|
-
end
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
def edit
|
236
|
-
|
237
|
-
@post = Post.find_by(id: params[:id])
|
238
|
-
|
239
|
-
end
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
def update
|
244
|
-
|
245
|
-
@post = Post.find_by(id: params[:id])
|
246
|
-
|
247
|
-
@post.content = params[:content]
|
248
|
-
|
249
|
-
if @post.save
|
250
|
-
|
251
|
-
flash[:notice] = "投稿を編集しました"
|
252
|
-
|
253
|
-
redirect_to("/posts/index")
|
254
|
-
|
255
|
-
else
|
256
|
-
|
257
|
-
render("/posts/edit")
|
258
|
-
|
259
|
-
end
|
260
|
-
|
261
|
-
end
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
def destroy
|
266
|
-
|
267
|
-
@post = Post.find_by(id: params[:id])
|
268
|
-
|
269
|
-
@post.destroy
|
270
|
-
|
271
|
-
flash[:notice] ="投稿を削除しました"
|
272
|
-
|
273
|
-
redirect_to("/posts/index")
|
274
|
-
|
275
|
-
end
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
end
|
280
|
-
|
281
|
-
```
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
### 該当のソースコード
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
```HTML
|
290
|
-
|
291
|
-
<div class="main posts-index">
|
292
|
-
|
293
|
-
<div class="container">
|
294
|
-
|
295
|
-
<% @posts.each do |post| %>
|
296
|
-
|
297
|
-
<div class="posts-index-item">
|
298
|
-
|
299
|
-
<div class="post-left">
|
300
|
-
|
301
|
-
<img src="<%= "/post.user_images/#{user.image_name}" %>">
|
302
|
-
|
303
|
-
</div>
|
304
|
-
|
305
|
-
<div class="post-right">
|
306
|
-
|
307
|
-
<div class="post-user-name">
|
308
|
-
|
309
|
-
<%= link_to(post.user.name, "/users/#{post.user.id}") %>
|
310
|
-
|
311
|
-
</div>
|
312
|
-
|
313
|
-
<%= link_to(post.content, "/posts/#{post.id}") %>
|
314
|
-
|
315
|
-
</div>
|
316
|
-
|
317
|
-
</div>
|
318
|
-
|
319
|
-
<% end %>
|
320
|
-
|
321
|
-
</div>
|
322
|
-
|
323
|
-
</div>
|
324
|
-
|
325
|
-
```
|
326
|
-
|
327
|
-
|
328
|
-
|
329
391
|
### 試したこと
|
330
392
|
|
331
|
-
|
393
|
+
投稿の詳細画面を表示しようとしたところそれでもメソッドエラーが出たので
|
332
|
-
|
394
|
+
|
333
|
-
|
395
|
+
ビュー以外にミスがあるのかと思いました。
|