質問編集履歴

9

ソースコードを修正致しました。

2020/09/23 14:28

投稿

Tarzan3154
Tarzan3154

スコア7

test CHANGED
File without changes
test CHANGED
@@ -484,7 +484,7 @@
484
484
 
485
485
  </ul>
486
486
 
487
- <%= render 'users', users: @likes %>
487
+ <%= render 'users', users: @favorites %>
488
488
 
489
489
  </div>
490
490
 

8

viewファイルのソースコードを追記致しました。

2020/09/23 14:27

投稿

Tarzan3154
Tarzan3154

スコア7

test CHANGED
File without changes
test CHANGED
@@ -440,6 +440,58 @@
440
440
 
441
441
  ```
442
442
 
443
+ ```
444
+
445
+ likes.html.erb
446
+
447
+
448
+
449
+ <div class="row">
450
+
451
+ <aside class="col-sm-4">
452
+
453
+ <div class="card">
454
+
455
+ <div class="card-header">
456
+
457
+ <h3 class="card-title"><%= @user.name %></h3>
458
+
459
+ </div>
460
+
461
+ <div class="card-body">
462
+
463
+ <img class="rounded img-fluid" src="<%= gravatar_url(@user, { size: 500 }) %>" alt="">
464
+
465
+ </div>
466
+
467
+ </div>
468
+
469
+ <%= render 'relationships/follow_button', user: @user %>
470
+
471
+ </aside>
472
+
473
+ <div class="col-sm-8">
474
+
475
+ <ul class="nav nav-tabs nav-justified mb-3">
476
+
477
+ <li class="nav-item"><a href="<%= likes_user_path(@user) %>" class="nav-link <%= 'active' if current_page?(likes_user_path(@user)) %>">Favorites <span class="badge badge-secondary"><%= @count_favorites %></span></a></li>
478
+
479
+ <li class="nav-item"><a href="<%= user_path(@user) %>" class="nav-link <%= 'active' if current_page?(user_path(@user)) %>">Microposts <span class="badge badge-secondary"><%= @count_microposts %></span></a></li>
480
+
481
+ <li class="nav-item"><a href="<%= followings_user_path(@user) %>" class="nav-link <%= 'active' if current_page?(followings_user_path(@user)) %>">Followings <span class="badge badge-secondary"><%= @count_followings %></span></a></li>
482
+
483
+ <li class="nav-item"><a href="<%= followers_user_path(@user) %>" class="nav-link <%= 'active' if current_page?(followers_user_path(@user)) %>">Followers <span class="badge badge-secondary"><%= @count_followers %></span></a></li>
484
+
485
+ </ul>
486
+
487
+ <%= render 'users', users: @likes %>
488
+
489
+ </div>
490
+
491
+ </div>
492
+
493
+ ```
494
+
443
495
 
444
496
 
445
497
  ### 試したこと

7

前提・実現したいことを編集しました。

2020/09/22 14:39

投稿

Tarzan3154
Tarzan3154

スコア7

test CHANGED
File without changes
test CHANGED
@@ -2,6 +2,8 @@
2
2
 
3
3
 
4
4
 
5
+ プログラミング初心者です。どなたか教えてくださいm_ _m
6
+
5
7
  Railsで既に作成しておいたTwitterクローンに作成したMicropost(投稿)をUserがお気に入りに登録できる機能を追加することにしました。(お気に入り一覧を表示するページを作成し、ナビバーから自分のお気に入り一覧ページへのリンクをつけ、users#show にそれぞれのUserのお気に入り一覧へのリンクをつける)
6
8
 
7
9
  https://tarzanposts.herokuapp.com/

6

ソースコードを修正致しました。

2020/09/17 06:34

投稿

Tarzan3154
Tarzan3154

スコア7

test CHANGED
File without changes
test CHANGED
@@ -362,6 +362,82 @@
362
362
 
363
363
  ```
364
364
 
365
+ ```
366
+
367
+ index.html.erb
368
+
369
+
370
+
371
+ <% if logged_in? %>
372
+
373
+ <div class="row">
374
+
375
+ <aside class="col-sm-4">
376
+
377
+ <%= form_with(model: @micropost, local: true) do |f| %>
378
+
379
+ <div class="form-group">
380
+
381
+ <%= f.text_area :content, class: 'form-control', rows: 5 %>
382
+
383
+ </div>
384
+
385
+ <%= f.submit 'Post', class: 'btn btn-primary btn-block' %>
386
+
387
+ <% end %>
388
+
389
+ </aside>
390
+
391
+ <div class="col-sm-8">
392
+
393
+ <%= render 'microposts/microposts', microposts: @microposts %>
394
+
395
+ </div>
396
+
397
+ </div>
398
+
399
+ <% else %>
400
+
401
+ <div class="center jumbotron">
402
+
403
+ <div class="text-center">
404
+
405
+ <h1>Welcome to the Microposts</h1>
406
+
407
+ <%= link_to 'Sign up now!', signup_path, class: 'btn btn-lg btn-primary' %>
408
+
409
+ </div>
410
+
411
+ </div>
412
+
413
+ <% end %>
414
+
415
+ ```
416
+
417
+ ```
418
+
419
+ toppages_controller.rb
420
+
421
+
422
+
423
+ class ToppagesController < ApplicationController
424
+
425
+ def index
426
+
427
+ if logged_in?
428
+
429
+ @micropost = current_user.microposts.build # form_with 用
430
+
431
+ @microposts = current_user.feed_microposts.order(id: :desc).page(params[:page])
432
+
433
+ end
434
+
435
+ end
436
+
437
+ end
438
+
439
+ ```
440
+
365
441
 
366
442
 
367
443
  ### 試したこと

5

エラーメッセージ・ソースコードを追記致しました。

2020/09/14 08:18

投稿

Tarzan3154
Tarzan3154

スコア7

test CHANGED
File without changes
test CHANGED
@@ -16,6 +16,12 @@
16
16
 
17
17
  ###②お気に入りに登録しているMicropostを削除しようとすると、エラーメッセージが表示されて削除できない。(お気に入りに登録していないMicropostは正常に削除できる)
18
18
 
19
+ ###③_navbar.html.erb で 
20
+
21
+ Favoritesのhref属性を <%= likes_user_path(@user) %>
22
+
23
+ にして、ログアウトしてToppages/indexの画面を表示させようとすると、下記のエラー画面が表示されてしまう。(href属性を / にしたら、正常に表示される)
24
+
19
25
  ### 発生している問題・エラーメッセージ
20
26
 
21
27
 
@@ -70,115 +76,149 @@
70
76
 
71
77
  ```
72
78
 
79
+ ```3
80
+
81
+ ActionController::UrlGenerationError in Toppages#index
82
+
83
+
84
+
85
+ No route matches {:action=>"likes", :controller=>"users", :id=>nil}, missing required keys: [:id]
86
+
87
+
88
+
89
+ Extracted source (around line #7):
90
+
91
+ <span class="navbar-toggler-icon"></span>
92
+
93
+ </button>
94
+
95
+ <a class="navbar-brand" href="<%= likes_user_path(@user) %>">Favorites</a>
96
+
97
+ <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent">
98
+
99
+ <span class="navbar-toggler-icon"></span>
100
+
101
+ </button>
102
+
103
+ ```
104
+
73
105
 
74
106
 
75
107
  ### 該当のソースコード
76
108
 
77
109
 
78
110
 
79
- ```Ruby
111
+ ```
80
-
112
+
81
- user.rb
113
+ users_controller.rb
82
-
83
-
84
-
114
+
115
+
116
+
85
- class User < ApplicationRecord
117
+ class UsersController < ApplicationController
118
+
86
-
119
+ before_action :require_user_logged_in, only: [:index, :show, :followings, :followers, :likes]
120
+
121
+
122
+
123
+ def index
124
+
125
+ @users = User.order(id: :desc).page(params[:page]).per(25)
126
+
127
+ end
128
+
129
+
130
+
131
+ def show
132
+
87
- before_save { self.email.downcase! }
133
+ @user = User.find(params[:id])
134
+
88
-
135
+ @microposts = @user.microposts.order(id: :desc).page(params[:page])
136
+
137
+ counts(@user)
138
+
89
- validates :name, presence: true, length: { maximum: 50 }
139
+ @favorites = @user.favorites.page(params[:page])
140
+
90
-
141
+ counts(@user)
142
+
143
+ end
144
+
145
+
146
+
147
+ def new
148
+
91
- validates :email, presence: true, length: { maximum: 255 },
149
+ @user = User.new
150
+
92
-
151
+ end
152
+
153
+
154
+
93
- format: { with: /\A[\w+\-.]+@[a-z\d\-.]+.[a-z]+\z/i },
155
+ def create
94
-
156
+
95
- uniqueness: { case_sensitive: false }
157
+ @user = User.new(user_params)
158
+
159
+
160
+
96
-
161
+ if @user.save
162
+
163
+ flash[:success] = 'ユーザを登録しました。'
164
+
97
- has_secure_password
165
+ redirect_to @user
166
+
167
+ else
168
+
169
+ flash.now[:danger] = 'ユーザの登録に失敗しました。'
170
+
171
+ render :new
172
+
173
+ end
174
+
175
+ end
98
176
 
99
177
 
100
178
 
101
- has_many :microposts
179
+ def followings
102
-
180
+
103
- has_many :relationships
181
+ @user = User.find(params[:id])
104
-
182
+
105
- has_many :followings, through: :relationships, source: :follow
183
+ @followings = @user.followings.page(params[:page])
106
-
107
- has_many :reverses_of_relationship, class_name: 'Relationship', foreign_key: 'follow_id'
184
+
108
-
109
- has_many :followers, through: :reverses_of_relationship, source: :user
185
+ counts(@user)
186
+
187
+ end
110
188
 
111
189
 
112
190
 
113
- has_many :favorites
191
+ def followers
114
-
192
+
115
- has_many :favorite_microposts, through: :favorites, source: :micropost
193
+ @user = User.find(params[:id])
116
-
117
- # has_many :reverses_of_favorite, class_name: 'Favorite', foreign_key: 'micropost_id'
194
+
118
-
119
- # has_many :favorites, through: :reverses_of_favorite, source: :user
195
+ @followers = @user.followers.page(params[:page])
196
+
197
+ counts(@user)
198
+
199
+ end
120
200
 
121
201
 
122
202
 
203
+ def likes
204
+
205
+ @user = User.find(params[:id])
206
+
207
+ @favorites = @user.favorites.page(params[:page])
208
+
123
- def follow(other_user)
209
+ counts(@user)
124
-
125
- unless self == other_user
126
-
127
- self.relationships.find_or_create_by(follow_id: other_user.id)
128
-
129
- end
130
-
131
- end
132
-
133
-
134
-
135
- def unfollow(other_user)
136
-
137
- relationship = self.relationships.find_by(follow_id: other_user.id)
138
-
139
- relationship.destroy if relationship
140
-
141
- end
142
-
143
-
144
-
145
- def following?(other_user)
146
-
147
- self.followings.include?(other_user)
148
210
 
149
211
  end
150
212
 
151
213
 
152
214
 
215
+ private
216
+
217
+
218
+
153
- def feed_microposts
219
+ def user_params
154
-
155
- Micropost.where(user_id: self.following_ids + [self.id])
220
+
156
-
157
- end
158
-
159
-
160
-
161
- def favorite(micropost)
162
-
163
- self.favorites.find_or_create_by(micropost_id: micropost.id)
164
-
165
- end
166
-
167
-
168
-
169
- def unfavorite(micropost)
170
-
171
- favorite = self.favorites.find_by(micropost_id: micropost.id)
221
+ params.require(:user).permit(:name, :email, :password, :password_confirmation)
172
-
173
- favorite.destroy if favorite
174
-
175
- end
176
-
177
-
178
-
179
- def favorites?(micropost)
180
-
181
- self.favorite_microposts.include?(micropost)
182
222
 
183
223
  end
184
224
 
@@ -188,325 +228,137 @@
188
228
 
189
229
  ```
190
230
 
191
- show.html.erb
231
+ application.html.erb
232
+
233
+
234
+
192
-
235
+ <!DOCTYPE html>
193
-
194
-
236
+
195
- <div class="row">
237
+ <html lang="ja">
238
+
196
-
239
+ <head>
240
+
197
- <aside class="col-sm-4">
241
+ <meta charset="UTF-8">
242
+
198
-
243
+ <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
244
+
245
+ <title>Microposts</title>
246
+
247
+ <%= csrf_meta_tags %>
248
+
249
+ <%= csp_meta_tag %>
250
+
251
+
252
+
253
+ <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css">
254
+
255
+
256
+
257
+ <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
258
+
259
+ <%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>
260
+
261
+ <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
262
+
263
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.6/umd/popper.min.js"></script>
264
+
265
+ <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/js/bootstrap.min.js"></script>
266
+
267
+ <script defer src="https://use.fontawesome.com/releases/v5.7.2/js/all.js"></script>
268
+
269
+ </head>
270
+
271
+
272
+
273
+ <body>
274
+
275
+ <%= render 'layouts/navbar' %>
276
+
277
+
278
+
199
- <div class="card">
279
+ <div class="container">
200
-
280
+
201
- <div class="card-header">
281
+ <%= render 'layouts/flash_messages' %>
202
-
203
- <h3 class="card-title"><%= @user.name %></h3>
282
+
204
-
283
+
284
+
205
- </div>
285
+ <%= yield %>
206
-
207
- <div class="card-body">
208
-
209
- <img class="rounded img-fluid" src="<%= gravatar_url(@user, { size: 500 }) %>" alt="">
210
-
211
- </div>
212
286
 
213
287
  </div>
214
288
 
215
- <%= render 'relationships/follow_button', user: @user %>
216
-
217
- </aside>
218
-
219
- <div class="col-sm-8">
220
-
221
- <ul class="nav nav-tabs nav-justified mb-3">
222
-
223
- <li class="nav-item"><a href="<%= likes_user_path(@user) %>" class="nav-link <%= 'active' if current_page?(likes_user_path(@user)) %>">Favorites <span class="badge badge-secondary"><%= @count_favorites %></span></a></li>
224
-
225
- <li class="nav-item"><a href="<%= user_path(@user) %>" class="nav-link <%= 'active' if current_page?(user_path(@user)) %>">Microposts <span class="badge badge-secondary"><%= @count_microposts %></span></a></li>
226
-
227
- <li class="nav-item"><a href="<%= followings_user_path(@user) %>" class="nav-link <%= 'active' if current_page?(followings_user_path(@user)) %>">Followings <span class="badge badge-secondary"><%= @count_followings %></span></a></li>
228
-
229
- <li class="nav-item"><a href="<%= followers_user_path(@user) %>" class="nav-link <%= 'active' if current_page?(followers_user_path(@user)) %>">Followers <span class="badge badge-secondary"><%= @count_followers %></span></a></li>
230
-
231
- </ul>
232
-
233
- <%= render 'microposts/microposts', microposts: @microposts %>
234
-
235
- </div>
236
-
237
- </div>
238
-
239
- ```
240
-
241
- ```
242
-
243
- likes.html.erb
244
-
245
-
246
-
247
- <div class="row">
248
-
249
- <aside class="col-sm-4">
250
-
251
- <div class="card">
252
-
253
- <div class="card-header">
254
-
255
- <h3 class="card-title"><%= @user.name %></h3>
256
-
257
- </div>
258
-
259
- <div class="card-body">
260
-
261
- <img class="rounded img-fluid" src="<%= gravatar_url(@user, { size: 500 }) %>" alt="">
262
-
263
- </div>
289
+ </body>
290
+
291
+ </html>
292
+
293
+ ```
294
+
295
+ ```
296
+
297
+ _navbar.html.erb
298
+
299
+
300
+
301
+ <header class="mb-4">
302
+
303
+ <nav class="navbar navbar-expand-sm navbar-dark bg-dark">
304
+
305
+ <a class="navbar-brand" href="/">Microposts</a>
306
+
307
+ <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent">
308
+
309
+ <span class="navbar-toggler-icon"></span>
310
+
311
+ </button>
312
+
313
+ <a class="navbar-brand" href="<%= likes_user_path(@user) %>">Favorites</a>
314
+
315
+ <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent">
316
+
317
+ <span class="navbar-toggler-icon"></span>
318
+
319
+ </button>
320
+
321
+
322
+
323
+ <div class="collapse navbar-collapse justify-content-end" id="navbarSupportedContent">
324
+
325
+ <ul class="navbar-nav">
326
+
327
+ <% if logged_in? %>
328
+
329
+ <li class="nav-item"><%= link_to 'Users', users_path, class: 'nav-link' %></li>
330
+
331
+ <li class="nav-item dropdown">
332
+
333
+ <a href="#" class="nav-link dropdown-toggle" data-toggle="dropdown"><%= current_user.name %></a>
334
+
335
+ <ul class="dropdown-menu dropdown-menu-right">
336
+
337
+ <li class="dropdown-item"><%= link_to 'My profile', user_path(current_user) %></li>
338
+
339
+ <li class="dropdown-divider"></li>
340
+
341
+ <li class="dropdown-item"><%= link_to 'Logout', logout_path, method: :delete %></li>
342
+
343
+ </ul>
344
+
345
+ </li>
346
+
347
+ <% else %>
348
+
349
+ <li class="nav-item"><%= link_to 'Signup', signup_path, class: 'nav-link' %></li>
350
+
351
+ <li class="nav-item"><%= link_to 'Login', login_path, class: 'nav-link' %></li>
352
+
353
+ <% end %>
354
+
355
+ </ul>
264
356
 
265
357
  </div>
266
358
 
267
- <%= render 'relationships/follow_button', user: @user %>
268
-
269
- </aside>
270
-
271
- <div class="col-sm-8">
272
-
273
- <ul class="nav nav-tabs nav-justified mb-3">
274
-
275
- <li class="nav-item"><a href="<%= likes_user_path(@user) %>" class="nav-link <%= 'active' if current_page?(likes_user_path(@user)) %>">Favorites <span class="badge badge-secondary"><%= @count_favorites %></span></a></li>
276
-
277
- <li class="nav-item"><a href="<%= user_path(@user) %>" class="nav-link <%= 'active' if current_page?(user_path(@user)) %>">Microposts <span class="badge badge-secondary"><%= @count_microposts %></span></a></li>
278
-
279
- <li class="nav-item"><a href="<%= followings_user_path(@user) %>" class="nav-link <%= 'active' if current_page?(followings_user_path(@user)) %>">Followings <span class="badge badge-secondary"><%= @count_followings %></span></a></li>
280
-
281
- <li class="nav-item"><a href="<%= followers_user_path(@user) %>" class="nav-link <%= 'active' if current_page?(followers_user_path(@user)) %>">Followers <span class="badge badge-secondary"><%= @count_followers %></span></a></li>
282
-
283
- </ul>
284
-
285
- <%= render 'users', users: @favorites %>
286
-
287
- </div>
288
-
289
- </div>
290
-
291
- ```
292
-
293
- ```
294
-
295
- users_controller.rb
296
-
297
-
298
-
299
- class UsersController < ApplicationController
300
-
301
- before_action :require_user_logged_in, only: [:index, :show, :followings, :followers, :likes]
302
-
303
-
304
-
305
- def index
306
-
307
- @users = User.order(id: :desc).page(params[:page]).per(25)
308
-
309
- end
310
-
311
-
312
-
313
- def show
314
-
315
- @user = User.find(params[:id])
316
-
317
- @microposts = @user.microposts.order(id: :desc).page(params[:page])
318
-
319
- counts(@user)
320
-
321
- @favorites = @user.favorites.page(params[:page])
322
-
323
- counts(@user)
324
-
325
- end
326
-
327
-
328
-
329
- def new
330
-
331
- @user = User.new
332
-
333
- end
334
-
335
-
336
-
337
- def create
338
-
339
- @user = User.new(user_params)
340
-
341
-
342
-
343
- if @user.save
344
-
345
- flash[:success] = 'ユーザを登録しました。'
346
-
347
- redirect_to @user
348
-
349
- else
350
-
351
- flash.now[:danger] = 'ユーザの登録に失敗しました。'
352
-
353
- render :new
354
-
355
- end
356
-
357
- end
358
-
359
-
360
-
361
- def followings
362
-
363
- @user = User.find(params[:id])
364
-
365
- @followings = @user.followings.page(params[:page])
366
-
367
- counts(@user)
368
-
369
- end
370
-
371
-
372
-
373
- def followers
374
-
375
- @user = User.find(params[:id])
376
-
377
- @followers = @user.followers.page(params[:page])
378
-
379
- counts(@user)
380
-
381
- end
382
-
383
-
384
-
385
- def likes
386
-
387
- @user = User.find(params[:id])
388
-
389
- @favorites = @user.favorites.page(params[:page])
390
-
391
- counts(@user)
392
-
393
- end
394
-
395
-
396
-
397
- private
398
-
399
-
400
-
401
- def user_params
402
-
403
- params.require(:user).permit(:name, :email, :password, :password_confirmation)
404
-
405
- end
406
-
407
- end
408
-
409
- ```
410
-
411
- ```
412
-
413
- favorite.rb
414
-
415
-
416
-
417
- class Favorite < ApplicationRecord
418
-
419
- belongs_to :user
420
-
421
- belongs_to :micropost
422
-
423
- end
424
-
425
- ```
426
-
427
- ```
428
-
429
- micropost.rb
430
-
431
-
432
-
433
- class Micropost < ApplicationRecord
434
-
435
- belongs_to :user
436
-
437
- has_many :favorites, class_name: 'User'
438
-
439
-
440
-
441
- validates :content, presence: true, length: { maximum: 255 }
442
-
443
- end
444
-
445
- ```
446
-
447
- ```
448
-
449
- _users.html.erb
450
-
451
-
452
-
453
- <% if users.any? %>
454
-
455
- <ul class="list-unstyled">
456
-
457
- <% users.each do |user| %>
458
-
459
- <li class="media">
460
-
461
- <img class="mr-2 rounded" src="<%= gravatar_url(user, { size: 50 }) %>" alt="">
462
-
463
- <div class="media-body">
464
-
465
- <div>
466
-
467
- <%= user.name %>
468
-
469
- </div>
470
-
471
- <div>
472
-
473
- <p><%= link_to 'View profile', user_path(user) %></p>
474
-
475
- </div>
476
-
477
- </div>
478
-
479
- </li>
480
-
481
- <% end %>
482
-
483
- </ul>
484
-
485
- <%= paginate users %>
486
-
487
- <% end %>
488
-
489
- ```
490
-
491
- ```
492
-
493
- users_helper.rb
494
-
495
-
496
-
497
- module UsersHelper
498
-
499
- def gravatar_url(user, options = { size: 80 })
500
-
501
- gravatar_id = Digest::MD5::hexdigest(user.email.downcase)
502
-
503
- size = options[:size]
504
-
505
- "https://secure.gravatar.com/avatar/#{gravatar_id}?s=#{size}"
506
-
507
- end
508
-
509
- end
359
+ </nav>
360
+
361
+ </header>
510
362
 
511
363
  ```
512
364
 
@@ -522,8 +374,6 @@
522
374
 
523
375
 
524
376
 
525
-
526
-
527
377
  ### 補足情報(FW/ツールのバージョンなど)
528
378
 
529
379
  DB:MySQL

4

ソースコードを修正致しました。

2020/09/08 09:20

投稿

Tarzan3154
Tarzan3154

スコア7

test CHANGED
File without changes
test CHANGED
@@ -444,6 +444,72 @@
444
444
 
445
445
  ```
446
446
 
447
+ ```
448
+
449
+ _users.html.erb
450
+
451
+
452
+
453
+ <% if users.any? %>
454
+
455
+ <ul class="list-unstyled">
456
+
457
+ <% users.each do |user| %>
458
+
459
+ <li class="media">
460
+
461
+ <img class="mr-2 rounded" src="<%= gravatar_url(user, { size: 50 }) %>" alt="">
462
+
463
+ <div class="media-body">
464
+
465
+ <div>
466
+
467
+ <%= user.name %>
468
+
469
+ </div>
470
+
471
+ <div>
472
+
473
+ <p><%= link_to 'View profile', user_path(user) %></p>
474
+
475
+ </div>
476
+
477
+ </div>
478
+
479
+ </li>
480
+
481
+ <% end %>
482
+
483
+ </ul>
484
+
485
+ <%= paginate users %>
486
+
487
+ <% end %>
488
+
489
+ ```
490
+
491
+ ```
492
+
493
+ users_helper.rb
494
+
495
+
496
+
497
+ module UsersHelper
498
+
499
+ def gravatar_url(user, options = { size: 80 })
500
+
501
+ gravatar_id = Digest::MD5::hexdigest(user.email.downcase)
502
+
503
+ size = options[:size]
504
+
505
+ "https://secure.gravatar.com/avatar/#{gravatar_id}?s=#{size}"
506
+
507
+ end
508
+
509
+ end
510
+
511
+ ```
512
+
447
513
 
448
514
 
449
515
  ### 試したこと

3

ソースコードを修正致しました。

2020/09/07 15:38

投稿

Tarzan3154
Tarzan3154

スコア7

test CHANGED
File without changes
test CHANGED
@@ -186,7 +186,57 @@
186
186
 
187
187
  ```
188
188
 
189
-
189
+ ```
190
+
191
+ show.html.erb
192
+
193
+
194
+
195
+ <div class="row">
196
+
197
+ <aside class="col-sm-4">
198
+
199
+ <div class="card">
200
+
201
+ <div class="card-header">
202
+
203
+ <h3 class="card-title"><%= @user.name %></h3>
204
+
205
+ </div>
206
+
207
+ <div class="card-body">
208
+
209
+ <img class="rounded img-fluid" src="<%= gravatar_url(@user, { size: 500 }) %>" alt="">
210
+
211
+ </div>
212
+
213
+ </div>
214
+
215
+ <%= render 'relationships/follow_button', user: @user %>
216
+
217
+ </aside>
218
+
219
+ <div class="col-sm-8">
220
+
221
+ <ul class="nav nav-tabs nav-justified mb-3">
222
+
223
+ <li class="nav-item"><a href="<%= likes_user_path(@user) %>" class="nav-link <%= 'active' if current_page?(likes_user_path(@user)) %>">Favorites <span class="badge badge-secondary"><%= @count_favorites %></span></a></li>
224
+
225
+ <li class="nav-item"><a href="<%= user_path(@user) %>" class="nav-link <%= 'active' if current_page?(user_path(@user)) %>">Microposts <span class="badge badge-secondary"><%= @count_microposts %></span></a></li>
226
+
227
+ <li class="nav-item"><a href="<%= followings_user_path(@user) %>" class="nav-link <%= 'active' if current_page?(followings_user_path(@user)) %>">Followings <span class="badge badge-secondary"><%= @count_followings %></span></a></li>
228
+
229
+ <li class="nav-item"><a href="<%= followers_user_path(@user) %>" class="nav-link <%= 'active' if current_page?(followers_user_path(@user)) %>">Followers <span class="badge badge-secondary"><%= @count_followers %></span></a></li>
230
+
231
+ </ul>
232
+
233
+ <%= render 'microposts/microposts', microposts: @microposts %>
234
+
235
+ </div>
236
+
237
+ </div>
238
+
239
+ ```
190
240
 
191
241
  ```
192
242
 
@@ -358,9 +408,41 @@
358
408
 
359
409
  ```
360
410
 
361
-
411
+ ```
412
+
362
-
413
+ favorite.rb
414
+
415
+
416
+
363
-
417
+ class Favorite < ApplicationRecord
418
+
419
+ belongs_to :user
420
+
421
+ belongs_to :micropost
422
+
423
+ end
424
+
425
+ ```
426
+
427
+ ```
428
+
429
+ micropost.rb
430
+
431
+
432
+
433
+ class Micropost < ApplicationRecord
434
+
435
+ belongs_to :user
436
+
437
+ has_many :favorites, class_name: 'User'
438
+
439
+
440
+
441
+ validates :content, presence: true, length: { maximum: 255 }
442
+
443
+ end
444
+
445
+ ```
364
446
 
365
447
 
366
448
 

2

ソースコードを修正致しました。

2020/09/06 12:43

投稿

Tarzan3154
Tarzan3154

スコア7

test CHANGED
File without changes
test CHANGED
@@ -78,33 +78,53 @@
78
78
 
79
79
  ```Ruby
80
80
 
81
- routes.rb
81
+ user.rb
82
-
83
-
84
-
85
- Rails.application.routes.draw do
82
+
86
-
83
+
84
+
87
- root to: "toppages#index"
85
+ class User < ApplicationRecord
88
-
89
-
90
-
86
+
91
- get 'login', to: 'sessions#new'
87
+ before_save { self.email.downcase! }
92
-
88
+
93
- post 'login', to: 'sessions#create'
89
+ validates :name, presence: true, length: { maximum: 50 }
94
-
95
- delete 'logout', to: 'sessions#destroy'
90
+
96
-
97
-
98
-
99
- get 'signup', to: 'users#new'
100
-
101
- resources :users, only: [:index, :show, :new, :create] do
91
+ validates :email, presence: true, length: { maximum: 255 },
92
+
102
-
93
+ format: { with: /\A[\w+\-.]+@[a-z\d\-.]+.[a-z]+\z/i },
94
+
95
+ uniqueness: { case_sensitive: false }
96
+
97
+ has_secure_password
98
+
99
+
100
+
103
- member do
101
+ has_many :microposts
102
+
104
-
103
+ has_many :relationships
104
+
105
+ has_many :followings, through: :relationships, source: :follow
106
+
107
+ has_many :reverses_of_relationship, class_name: 'Relationship', foreign_key: 'follow_id'
108
+
109
+ has_many :followers, through: :reverses_of_relationship, source: :user
110
+
111
+
112
+
105
- get :followings
113
+ has_many :favorites
114
+
106
-
115
+ has_many :favorite_microposts, through: :favorites, source: :micropost
116
+
117
+ # has_many :reverses_of_favorite, class_name: 'Favorite', foreign_key: 'micropost_id'
118
+
119
+ # has_many :favorites, through: :reverses_of_favorite, source: :user
120
+
121
+
122
+
107
- get :followers
123
+ def follow(other_user)
124
+
125
+ unless self == other_user
126
+
127
+ self.relationships.find_or_create_by(follow_id: other_user.id)
108
128
 
109
129
  end
110
130
 
@@ -112,279 +132,235 @@
112
132
 
113
133
 
114
134
 
115
- resources :users, only: [:index, :show, :new, :create] do
116
-
117
- member do
118
-
119
- get :likes
135
+ def unfollow(other_user)
136
+
137
+ relationship = self.relationships.find_by(follow_id: other_user.id)
138
+
139
+ relationship.destroy if relationship
140
+
141
+ end
142
+
143
+
144
+
145
+ def following?(other_user)
146
+
147
+ self.followings.include?(other_user)
148
+
149
+ end
150
+
151
+
152
+
153
+ def feed_microposts
154
+
155
+ Micropost.where(user_id: self.following_ids + [self.id])
156
+
157
+ end
158
+
159
+
160
+
161
+ def favorite(micropost)
162
+
163
+ self.favorites.find_or_create_by(micropost_id: micropost.id)
164
+
165
+ end
166
+
167
+
168
+
169
+ def unfavorite(micropost)
170
+
171
+ favorite = self.favorites.find_by(micropost_id: micropost.id)
172
+
173
+ favorite.destroy if favorite
174
+
175
+ end
176
+
177
+
178
+
179
+ def favorites?(micropost)
180
+
181
+ self.favorite_microposts.include?(micropost)
182
+
183
+ end
184
+
185
+ end
186
+
187
+ ```
188
+
189
+
190
+
191
+ ```
192
+
193
+ likes.html.erb
194
+
195
+
196
+
197
+ <div class="row">
198
+
199
+ <aside class="col-sm-4">
200
+
201
+ <div class="card">
202
+
203
+ <div class="card-header">
204
+
205
+ <h3 class="card-title"><%= @user.name %></h3>
206
+
207
+ </div>
208
+
209
+ <div class="card-body">
210
+
211
+ <img class="rounded img-fluid" src="<%= gravatar_url(@user, { size: 500 }) %>" alt="">
212
+
213
+ </div>
214
+
215
+ </div>
216
+
217
+ <%= render 'relationships/follow_button', user: @user %>
218
+
219
+ </aside>
220
+
221
+ <div class="col-sm-8">
222
+
223
+ <ul class="nav nav-tabs nav-justified mb-3">
224
+
225
+ <li class="nav-item"><a href="<%= likes_user_path(@user) %>" class="nav-link <%= 'active' if current_page?(likes_user_path(@user)) %>">Favorites <span class="badge badge-secondary"><%= @count_favorites %></span></a></li>
226
+
227
+ <li class="nav-item"><a href="<%= user_path(@user) %>" class="nav-link <%= 'active' if current_page?(user_path(@user)) %>">Microposts <span class="badge badge-secondary"><%= @count_microposts %></span></a></li>
228
+
229
+ <li class="nav-item"><a href="<%= followings_user_path(@user) %>" class="nav-link <%= 'active' if current_page?(followings_user_path(@user)) %>">Followings <span class="badge badge-secondary"><%= @count_followings %></span></a></li>
230
+
231
+ <li class="nav-item"><a href="<%= followers_user_path(@user) %>" class="nav-link <%= 'active' if current_page?(followers_user_path(@user)) %>">Followers <span class="badge badge-secondary"><%= @count_followers %></span></a></li>
232
+
233
+ </ul>
234
+
235
+ <%= render 'users', users: @favorites %>
236
+
237
+ </div>
238
+
239
+ </div>
240
+
241
+ ```
242
+
243
+ ```
244
+
245
+ users_controller.rb
246
+
247
+
248
+
249
+ class UsersController < ApplicationController
250
+
251
+ before_action :require_user_logged_in, only: [:index, :show, :followings, :followers, :likes]
252
+
253
+
254
+
255
+ def index
256
+
257
+ @users = User.order(id: :desc).page(params[:page]).per(25)
258
+
259
+ end
260
+
261
+
262
+
263
+ def show
264
+
265
+ @user = User.find(params[:id])
266
+
267
+ @microposts = @user.microposts.order(id: :desc).page(params[:page])
268
+
269
+ counts(@user)
270
+
271
+ @favorites = @user.favorites.page(params[:page])
272
+
273
+ counts(@user)
274
+
275
+ end
276
+
277
+
278
+
279
+ def new
280
+
281
+ @user = User.new
282
+
283
+ end
284
+
285
+
286
+
287
+ def create
288
+
289
+ @user = User.new(user_params)
290
+
291
+
292
+
293
+ if @user.save
294
+
295
+ flash[:success] = 'ユーザを登録しました。'
296
+
297
+ redirect_to @user
298
+
299
+ else
300
+
301
+ flash.now[:danger] = 'ユーザの登録に失敗しました。'
302
+
303
+ render :new
120
304
 
121
305
  end
122
306
 
123
307
  end
124
308
 
309
+
310
+
125
-
311
+ def followings
312
+
126
-
313
+ @user = User.find(params[:id])
314
+
315
+ @followings = @user.followings.page(params[:page])
316
+
317
+ counts(@user)
318
+
319
+ end
320
+
321
+
322
+
323
+ def followers
324
+
325
+ @user = User.find(params[:id])
326
+
127
- resources :microposts, only: [:create, :destroy]
327
+ @followers = @user.followers.page(params[:page])
328
+
128
-
329
+ counts(@user)
330
+
331
+ end
332
+
333
+
334
+
335
+ def likes
336
+
129
- resources :relationships, only: [:create, :destroy]
337
+ @user = User.find(params[:id])
130
-
338
+
131
- resources :favorites, only: [:create, :destroy]
339
+ @favorites = @user.favorites.page(params[:page])
340
+
341
+ counts(@user)
342
+
343
+ end
344
+
345
+
346
+
347
+ private
348
+
349
+
350
+
351
+ def user_params
352
+
353
+ params.require(:user).permit(:name, :email, :password, :password_confirmation)
354
+
355
+ end
132
356
 
133
357
  end
134
358
 
135
359
  ```
136
360
 
137
- ```
138
-
139
- users_helper.rb
140
-
141
-
142
-
143
- module UsersHelper
144
-
145
- def gravatar_url(user, options = { size: 80 })
146
-
147
- gravatar_id = Digest::MD5::hexdigest(user.email.downcase)
148
-
149
- size = options[:size]
150
-
151
- "https://secure.gravatar.com/avatar/#{gravatar_id}?s=#{size}"
152
-
153
- end
154
-
155
- end
156
-
157
- ```
158
-
159
- ```
160
-
161
- _favorite_button.html.erb
162
-
163
-
164
-
165
- <% if current_user.favorites?(micropost) %>
166
-
167
- <%= form_with(model: current_user.favorites.find_by(micropost_id: micropost.id), local: true, method: :delete) do |f| %>
168
-
169
- <%= hidden_field_tag :micropost_id %>
170
-
171
- <%= f.submit 'Unfavorite', class: 'btn btn-danger btn-sm' %>
172
-
173
- <% end %>
174
-
175
- <% else %>
176
-
177
- <%= form_with(model: current_user.favorites.build, local: true) do |f| %>
178
-
179
- <%= hidden_field_tag :micropost_id %>
180
-
181
- <%= f.submit 'Favorite', class: 'btn btn-primary btn-sm' %>
182
-
183
- <% end %>
184
-
185
- <% end %>
186
-
187
- ```
188
-
189
- ```
190
-
191
- _micropost.html.erb
192
-
193
-
194
-
195
- <ul class="list-unstyled">
196
-
197
- <% microposts.each do |micropost| %>
198
-
199
- <li class="media mb-3">
200
-
201
- <img class="mr-2 rounded" src="<%= gravatar_url(micropost.user, { size: 50 }) %>" alt="">
202
-
203
- <div class="media-body">
204
-
205
- <div>
206
-
207
- <%= link_to micropost.user.name, user_path(micropost.user) %> <span class="text-muted">posted at <%= micropost.created_at %></span>
208
-
209
- </div>
210
-
211
- <div>
212
-
213
- <p><%= micropost.content %></p>
214
-
215
- </div>
216
-
217
- <div class="row">
218
-
219
- <% if current_user == micropost.user %>
220
-
221
- <%= link_to "Delete", micropost, method: :delete, data: { confirm: "You sure?" }, class: "btn btn-danger btn-sm" %>
222
-
223
- <% end %>
224
-
225
- <%= render "favorites/favorite_button", micropost: micropost %>
226
-
227
- </div>
228
-
229
- </div>
230
-
231
- </li>
232
-
233
- <% end %>
234
-
235
- <%= paginate microposts %>
236
-
237
- </ul>
238
-
239
- ```
240
-
241
- ```
242
-
243
- _users.html.erb
244
-
245
-
246
-
247
- <% if users.any? %>
248
-
249
- <ul class="list-unstyled">
250
-
251
- <% users.each do |user| %>
252
-
253
- <li class="media">
254
-
255
- <img class="mr-2 rounded" src="<%= gravatar_url(user, { size: 50 }) %>" alt="">
256
-
257
- <div class="media-body">
258
-
259
- <div>
260
-
261
- <%= user.name %>
262
-
263
- </div>
264
-
265
- <div>
266
-
267
- <p><%= link_to 'View profile', user_path(user) %></p>
268
-
269
- </div>
270
-
271
- </div>
272
-
273
- </li>
274
-
275
- <% end %>
276
-
277
- </ul>
278
-
279
- <%= paginate users %>
280
-
281
- <% end %>
282
-
283
- ```
284
-
285
- ```
286
-
287
- show.html.erb
288
-
289
-
290
-
291
- <div class="row">
292
-
293
- <aside class="col-sm-4">
294
-
295
- <div class="card">
296
-
297
- <div class="card-header">
298
-
299
- <h3 class="card-title"><%= @user.name %></h3>
300
-
301
- </div>
302
-
303
- <div class="card-body">
304
-
305
- <img class="rounded img-fluid" src="<%= gravatar_url(@user, { size: 500 }) %>" alt="">
306
-
307
- </div>
308
-
309
- </div>
310
-
311
- <%= render 'relationships/follow_button', user: @user %>
312
-
313
- </aside>
314
-
315
- <div class="col-sm-8">
316
-
317
- <ul class="nav nav-tabs nav-justified mb-3">
318
-
319
- <li class="nav-item"><a href="<%= likes_user_path(@user) %>" class="nav-link <%= 'active' if current_page?(likes_user_path(@user)) %>">Favorites <span class="badge badge-secondary"><%= @count_favorites %></span></a></li>
320
-
321
- <li class="nav-item"><a href="<%= user_path(@user) %>" class="nav-link <%= 'active' if current_page?(user_path(@user)) %>">Microposts <span class="badge badge-secondary"><%= @count_microposts %></span></a></li>
322
-
323
- <li class="nav-item"><a href="<%= followings_user_path(@user) %>" class="nav-link <%= 'active' if current_page?(followings_user_path(@user)) %>">Followings <span class="badge badge-secondary"><%= @count_followings %></span></a></li>
324
-
325
- <li class="nav-item"><a href="<%= followers_user_path(@user) %>" class="nav-link <%= 'active' if current_page?(followers_user_path(@user)) %>">Followers <span class="badge badge-secondary"><%= @count_followers %></span></a></li>
326
-
327
- </ul>
328
-
329
- <%= render 'microposts/microposts', microposts: @microposts %>
330
-
331
- </div>
332
-
333
- </div>
334
-
335
- ```
336
-
337
- ```
338
-
339
- index.html.erb
340
-
341
-
342
-
343
- <% if logged_in? %>
344
-
345
- <div class="row">
346
-
347
- <aside class="col-sm-4">
348
-
349
- <%= form_with(model: @micropost, local: true) do |f| %>
350
-
351
- <div class="form-group">
352
-
353
- <%= f.text_area :content, class: 'form-control', rows: 5 %>
354
-
355
- </div>
356
-
357
- <%= f.submit 'Post', class: 'btn btn-primary btn-block' %>
358
-
359
- <% end %>
360
-
361
- </aside>
362
-
363
- <div class="col-sm-8">
364
-
365
- <%= render 'microposts/microposts', microposts: @microposts %>
366
-
367
- </div>
368
-
369
- </div>
370
-
371
- <% else %>
372
-
373
- <div class="center jumbotron">
374
-
375
- <div class="text-center">
376
-
377
- <h1>Welcome to the Microposts</h1>
378
-
379
- <%= link_to 'Sign up now!', signup_path, class: 'btn btn-lg btn-primary' %>
380
-
381
- </div>
382
-
383
- </div>
384
-
385
- <% end %>
386
-
387
- ```
361
+
362
+
363
+
388
364
 
389
365
 
390
366
 

1

試したこと・補足情報を追記しました。

2020/09/06 07:49

投稿

Tarzan3154
Tarzan3154

スコア7

test CHANGED
@@ -1 +1 @@
1
- Rails Micropostのお気に入り機能
1
+ Rails Micropostのお気に入り機能2
test CHANGED
@@ -390,7 +390,11 @@
390
390
 
391
391
  ### 試したこと
392
392
 
393
-
393
+ ①に関しては、fovoritesテーブルに"email"というカラムがないからかと思い、emailカラムを作成してみましたが、改善せず。(usersテーブルのmigrationファイル内でemailカラムの行に:null => falseをつけてみても変わりませんでした)
394
+
395
+ ②に関しては、お気に入り機能を実装する前は正常に動作しており、原因分かりませんでした。
396
+
397
+ ⇒microposts_controller.rbの記述等は間違いないでしょうか?
394
398
 
395
399
 
396
400
 
@@ -398,6 +402,12 @@
398
402
 
399
403
  ### 補足情報(FW/ツールのバージョンなど)
400
404
 
401
-
405
+ DB:MySQL
402
-
406
+
403
- ここにより詳細な情報を記載してください。
407
+ FW:Ruby on Rails5.2.2
408
+
409
+ コーディング環境:AWS Cloud9
410
+
411
+ バージョン管理システム:Git/GitHub
412
+
413
+ API用サーバー:Heroku