質問編集履歴
2
コードの書き直し
test
CHANGED
File without changes
|
test
CHANGED
@@ -203,3 +203,261 @@
|
|
203
203
|
</div>
|
204
204
|
|
205
205
|
```
|
206
|
+
|
207
|
+
|
208
|
+
|
209
|
+
```
|
210
|
+
|
211
|
+
app > views > prototype > _prototype.html.erb
|
212
|
+
|
213
|
+
|
214
|
+
|
215
|
+
<div class="card">
|
216
|
+
|
217
|
+
<%= link_to image_tag(prototype.image, class: :card__img ), prototype_path(prototype.id), method: :get %>
|
218
|
+
|
219
|
+
|
220
|
+
|
221
|
+
<div class="card__body">
|
222
|
+
|
223
|
+
<%= link_to prototype.title, prototype_path(prototype.id), method: :get, class: :card__title%>
|
224
|
+
|
225
|
+
<p class="card__summary">
|
226
|
+
|
227
|
+
<%= prototype.catch_copy %>
|
228
|
+
|
229
|
+
</p>
|
230
|
+
|
231
|
+
<%= link_to "by #{prototype.user.name}", "/users/#{prototype.user.id}", class: :card__user %>
|
232
|
+
|
233
|
+
</div>
|
234
|
+
|
235
|
+
</div>
|
236
|
+
|
237
|
+
二行目の
|
238
|
+
|
239
|
+
<%= link_to image_tag(prototype.image, class: :card__img ), prototype_path(prototype.id), method: :get %>
|
240
|
+
|
241
|
+
|
242
|
+
|
243
|
+
にてNameErrorが発生しました。
|
244
|
+
|
245
|
+
|
246
|
+
|
247
|
+
index.htmlは
|
248
|
+
|
249
|
+
|
250
|
+
|
251
|
+
|
252
|
+
|
253
|
+
<main class="main">
|
254
|
+
|
255
|
+
<div class="inner">
|
256
|
+
|
257
|
+
<%# ログインしているときは以下を表示する %>
|
258
|
+
|
259
|
+
<% if user_signed_in? %>
|
260
|
+
|
261
|
+
<div class="greeting">
|
262
|
+
|
263
|
+
こんにちは、
|
264
|
+
|
265
|
+
<%= link_to "#{current_user.name}さん", "/users/#{current_user.id}", class: :greeting__link %>
|
266
|
+
|
267
|
+
</div>
|
268
|
+
|
269
|
+
<% end %>
|
270
|
+
|
271
|
+
<%# // ログインしているときは上記を表示する %>
|
272
|
+
|
273
|
+
<div class="card__wrapper">
|
274
|
+
|
275
|
+
<%# 投稿機能実装後、部分テンプレートでプロトタイプ投稿一覧を表示する %>
|
276
|
+
|
277
|
+
<div class="prototypes">
|
278
|
+
|
279
|
+
<%= render partial: 'prototypes/prototype', collection: @prototypes %>
|
280
|
+
|
281
|
+
</div>
|
282
|
+
|
283
|
+
</div>
|
284
|
+
|
285
|
+
</div>
|
286
|
+
|
287
|
+
</main>
|
288
|
+
|
289
|
+
コントローラーは、
|
290
|
+
|
291
|
+
|
292
|
+
|
293
|
+
|
294
|
+
|
295
|
+
class PrototypesController < ApplicationController
|
296
|
+
|
297
|
+
before_action :authenticate_user!, except: [:index, :show]
|
298
|
+
|
299
|
+
|
300
|
+
|
301
|
+
def index
|
302
|
+
|
303
|
+
@prototypes = Prototype.all
|
304
|
+
|
305
|
+
end
|
306
|
+
|
307
|
+
|
308
|
+
|
309
|
+
def new
|
310
|
+
|
311
|
+
@prototype = Prototype.new
|
312
|
+
|
313
|
+
end
|
314
|
+
|
315
|
+
|
316
|
+
|
317
|
+
def create
|
318
|
+
|
319
|
+
@prototype = Prototype.new(prototype_params)
|
320
|
+
|
321
|
+
if @prototype.save
|
322
|
+
|
323
|
+
redirect_to root_path
|
324
|
+
|
325
|
+
else
|
326
|
+
|
327
|
+
render :new
|
328
|
+
|
329
|
+
end
|
330
|
+
|
331
|
+
end
|
332
|
+
|
333
|
+
|
334
|
+
|
335
|
+
def show
|
336
|
+
|
337
|
+
@prototype = Prototype.find(params[:id])
|
338
|
+
|
339
|
+
@comment = Comment.new
|
340
|
+
|
341
|
+
@comments = @prototype.comments.includes(:user)
|
342
|
+
|
343
|
+
end
|
344
|
+
|
345
|
+
|
346
|
+
|
347
|
+
def edit
|
348
|
+
|
349
|
+
@prototype =Prototype.find(params[:id])
|
350
|
+
|
351
|
+
end
|
352
|
+
|
353
|
+
|
354
|
+
|
355
|
+
def update
|
356
|
+
|
357
|
+
prototype = Prototype.find(params[:id])
|
358
|
+
|
359
|
+
if prototype.update(prototype_params)
|
360
|
+
|
361
|
+
redirect_to prototype_path
|
362
|
+
|
363
|
+
else
|
364
|
+
|
365
|
+
render :edit
|
366
|
+
|
367
|
+
end
|
368
|
+
|
369
|
+
end
|
370
|
+
|
371
|
+
|
372
|
+
|
373
|
+
def destroy
|
374
|
+
|
375
|
+
prototype = Prototype.find(params[:id])
|
376
|
+
|
377
|
+
prototype.destroy
|
378
|
+
|
379
|
+
redirect_to root_path
|
380
|
+
|
381
|
+
end
|
382
|
+
|
383
|
+
|
384
|
+
|
385
|
+
private
|
386
|
+
|
387
|
+
|
388
|
+
|
389
|
+
def prototype_params
|
390
|
+
|
391
|
+
params.require(:prototype).permit(:title, :catch_copy, :concept, :image).merge(user_id: current_user.id)
|
392
|
+
|
393
|
+
end
|
394
|
+
|
395
|
+
|
396
|
+
|
397
|
+
|
398
|
+
|
399
|
+
end
|
400
|
+
|
401
|
+
routes.rbは
|
402
|
+
|
403
|
+
|
404
|
+
|
405
|
+
|
406
|
+
|
407
|
+
Rails.application.routes.draw do
|
408
|
+
|
409
|
+
devise_for :users
|
410
|
+
|
411
|
+
root to: 'prototypes#index'
|
412
|
+
|
413
|
+
resources :prototypes do
|
414
|
+
|
415
|
+
resources :comments, only: :create
|
416
|
+
|
417
|
+
end
|
418
|
+
|
419
|
+
resources :users, only: :show
|
420
|
+
|
421
|
+
end
|
422
|
+
|
423
|
+
modelsは
|
424
|
+
|
425
|
+
|
426
|
+
|
427
|
+
|
428
|
+
|
429
|
+
class Prototype < ApplicationRecord
|
430
|
+
|
431
|
+
has_one_attached :image
|
432
|
+
|
433
|
+
has_many :comments
|
434
|
+
|
435
|
+
has_many :commnets, dependent: :destroy
|
436
|
+
|
437
|
+
has_many :image, dependent: :destroy
|
438
|
+
|
439
|
+
|
440
|
+
|
441
|
+
validates :title, presence: true
|
442
|
+
|
443
|
+
validates :catch_copy, presence: true
|
444
|
+
|
445
|
+
validates :concept, presence: true
|
446
|
+
|
447
|
+
validates :image, presence: true
|
448
|
+
|
449
|
+
|
450
|
+
|
451
|
+
belongs_to :user
|
452
|
+
|
453
|
+
end
|
454
|
+
|
455
|
+
|
456
|
+
|
457
|
+
```
|
458
|
+
|
459
|
+
|
460
|
+
|
461
|
+
こちらが詳細のコードになります。
|
462
|
+
|
463
|
+
宜しくお願いいたします。
|
1
コードの書き直し
test
CHANGED
File without changes
|
test
CHANGED
@@ -151,3 +151,55 @@
|
|
151
151
|
いまいち仕組みが掴めていません。
|
152
152
|
|
153
153
|
ご教授宜しくお願いします
|
154
|
+
|
155
|
+
|
156
|
+
|
157
|
+
@user.name〜に変更後
|
158
|
+
|
159
|
+
また別のエラーが出てきました。
|
160
|
+
|
161
|
+
|
162
|
+
|
163
|
+
NameError in Prototypes#index
|
164
|
+
|
165
|
+
Showing /Users/taigasoma/projects/protospace-30528/app/views/prototypes/_prototype.html.erb where line #2 raised:
|
166
|
+
|
167
|
+
|
168
|
+
|
169
|
+
uninitialized constant Prototype::Image
|
170
|
+
|
171
|
+
|
172
|
+
|
173
|
+
|
174
|
+
|
175
|
+
```
|
176
|
+
|
177
|
+
app > views > prototypes > _prototype.html.erb
|
178
|
+
|
179
|
+
|
180
|
+
|
181
|
+
<div class="card">
|
182
|
+
|
183
|
+
#下記がマークされたエラー文です。
|
184
|
+
|
185
|
+
<%= link_to image_tag(prototype.image, class: :card__img ) %>
|
186
|
+
|
187
|
+
|
188
|
+
|
189
|
+
<div class="card__body">
|
190
|
+
|
191
|
+
<%= link_to prototype.title, prototype_path(prototype.id), method: :get, class: :card__title%>
|
192
|
+
|
193
|
+
<p class="card__summary">
|
194
|
+
|
195
|
+
<%= prototype.catch_copy %>
|
196
|
+
|
197
|
+
</p>
|
198
|
+
|
199
|
+
<%= link_to "by #{prototype.user.name}", "/users/#{prototype.user.id}", class: :card__user %>
|
200
|
+
|
201
|
+
</div>
|
202
|
+
|
203
|
+
</div>
|
204
|
+
|
205
|
+
```
|