質問編集履歴

6

各コードを画像からコード挿入に変更しました!

2021/07/23 08:44

投稿

dodekamin.taro
dodekamin.taro

スコア0

test CHANGED
File without changes
test CHANGED
@@ -134,9 +134,7 @@
134
134
 
135
135
  ```ruby
136
136
 
137
- コード
138
-
139
- ```class UsersController < ApplicationController
137
+ コードclass UsersController < ApplicationController
140
138
 
141
139
 
142
140
 
@@ -248,15 +246,15 @@
248
246
 
249
247
  end
250
248
 
249
+ ```
250
+
251
251
 
252
252
 
253
253
  _info.html.erb
254
254
 
255
255
  ```html
256
256
 
257
- コード
258
-
259
- ```<table class='table'>
257
+ コード<table class='table'>
260
258
 
261
259
  <tr><%= attachment_image_tag user, :profile_image, :fill, 100, 100, fallback: "no-image-icon.jpg", class:"mt-3 rounded-circle"; %></tr>
262
260
 
@@ -340,6 +338,8 @@
340
338
 
341
339
  </div>
342
340
 
341
+ ```
342
+
343
343
  _
344
344
 
345
345
 
@@ -348,9 +348,7 @@
348
348
 
349
349
  ```html
350
350
 
351
- コード
352
-
353
- ```<div class="card-list-containar py-5">
351
+ コード<div class="card-list-containar py-5">
354
352
 
355
353
  <% @articles.each do |article| %>
356
354
 
@@ -406,13 +404,13 @@
406
404
 
407
405
 
408
406
 
407
+ ```
408
+
409
409
  relationship.rb
410
410
 
411
411
  ```ruby
412
412
 
413
- コード
414
-
415
- ```class Relationship < ApplicationRecord
413
+ コードclass Relationship < ApplicationRecord
416
414
 
417
415
 
418
416
 
@@ -434,15 +432,15 @@
434
432
 
435
433
  end
436
434
 
435
+ ```
436
+
437
437
 
438
438
 
439
439
  relationships_controller.rb
440
440
 
441
441
  ```ruby
442
442
 
443
- コード
444
-
445
- ```class RelationshipsController < ApplicationController
443
+ コードclass RelationshipsController < ApplicationController
446
444
 
447
445
 
448
446
 
@@ -476,6 +474,8 @@
476
474
 
477
475
 
478
476
 
477
+ ```
478
+
479
479
  ### 試したこと
480
480
 
481
481
  フォローの非同期化は以下の記事を参考に作成しております。

5

各コードを画像からコード挿入に変更しました!!

2021/07/23 08:44

投稿

dodekamin.taro
dodekamin.taro

スコア0

test CHANGED
File without changes
test CHANGED
@@ -50,199 +50,385 @@
50
50
 
51
51
  ```ruby
52
52
 
53
+ コードclass User < ApplicationRecord
54
+
55
+ # Include default devise modules. Others available are:
56
+
57
+ # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
58
+
59
+ devise :database_authenticatable, :registerable,
60
+
61
+ :recoverable, :rememberable, :validatable
62
+
63
+
64
+
65
+ has_many :articles
66
+
67
+ has_many :favorites, dependent: :destroy
68
+
69
+ attachment :profile_image, destroy: false
70
+
71
+
72
+
73
+ validates :name, length: { minimum: 2, maximum: 20 }, uniqueness: true
74
+
75
+ validates :profile, length: { maximum: 200 }
76
+
77
+
78
+
79
+ has_many :followed_relationships, foreign_key: "follower_id", class_name: "Relationship", dependent: :destroy
80
+
81
+ has_many :followed, through: :followed_relationships
82
+
83
+ has_many :follower_relationships, foreign_key: "followed_id", class_name: "Relationship", dependent: :destroy
84
+
85
+ has_many :followers, through: :follower_relationships
86
+
87
+
88
+
89
+ #フォローしているかを確認するメソッド
90
+
91
+ def followed?(user)
92
+
93
+ followed_relationships.find_by(followed_id: user.id)
94
+
95
+ end
96
+
97
+
98
+
99
+ #フォローするときのメソッド
100
+
101
+ def follow(user)
102
+
103
+ followed_relationships.create!(followed_id: user.id)
104
+
105
+ end
106
+
107
+
108
+
109
+ #フォローを外すときのメソッド
110
+
111
+ def unfollow(user)
112
+
113
+ followed_relationships.find_by(followed_id: user.id).destroy
114
+
115
+ end
116
+
117
+
118
+
119
+
120
+
121
+ end
122
+
123
+
124
+
125
+ ```
126
+
127
+
128
+
129
+
130
+
131
+
132
+
133
+ users_controller.rb
134
+
135
+ ```ruby
136
+
53
137
  コード
54
138
 
55
- ```class User < ApplicationRecord
56
-
57
- # Include default devise modules. Others available are:
58
-
59
- # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
60
-
61
- devise :database_authenticatable, :registerable,
62
-
63
- :recoverable, :rememberable, :validatable
64
-
65
-
66
-
67
- has_many :articles
68
-
69
- has_many :favorites, dependent: :destroy
70
-
71
- attachment :profile_image, destroy: false
72
-
73
-
74
-
75
- validates :name, length: { minimum: 2, maximum: 20 }, uniqueness: true
76
-
77
- validates :profile, length: { maximum: 200 }
78
-
79
-
80
-
81
- has_many :followed_relationships, foreign_key: "follower_id", class_name: "Relationship", dependent: :destroy
82
-
83
- has_many :followed, through: :followed_relationships
84
-
85
- has_many :follower_relationships, foreign_key: "followed_id", class_name: "Relationship", dependent: :destroy
86
-
87
- has_many :followers, through: :follower_relationships
88
-
89
-
90
-
91
- #フォローしているかを確認するメソッド
92
-
93
- def followed?(user)
94
-
95
- followed_relationships.find_by(followed_id: user.id)
96
-
97
- end
98
-
99
-
100
-
101
- #フォローするときのメソッド
102
-
103
- def follow(user)
104
-
105
- followed_relationships.create!(followed_id: user.id)
106
-
107
- end
108
-
109
-
110
-
111
- #フォローを外すときのメソッド
112
-
113
- def unfollow(user)
114
-
115
- followed_relationships.find_by(followed_id: user.id).destroy
116
-
117
- end
118
-
119
-
120
-
121
-
139
+ ```class UsersController < ApplicationController
140
+
141
+
142
+
143
+ before_action :authenticate_user!
144
+
145
+ before_action :ensure_correct_user, only: [:edit, :update]
146
+
147
+
148
+
149
+ def show
150
+
151
+ @user = User.find(params[:id])
152
+
153
+ @articles = @user.articles
154
+
155
+ # @article = Article.new
156
+
157
+ end
158
+
159
+
160
+
161
+ def index
162
+
163
+ @users = User.all
164
+
165
+ end
166
+
167
+
168
+
169
+ def edit
170
+
171
+ @user = User.find(params[:id])
172
+
173
+ end
174
+
175
+
176
+
177
+ def update
178
+
179
+ @user = User.find(params[:id])
180
+
181
+ if @user.update(user_params)
182
+
183
+ redirect_to user_path(@user), notice: "更新しました!"
184
+
185
+ else
186
+
187
+ render "edit"
188
+
189
+ end
190
+
191
+ end
192
+
193
+
194
+
195
+ def followed
196
+
197
+ #@userがフォローしているユーザー
198
+
199
+ @user = User.find(params[:id])
200
+
201
+ @users = @user.followed
202
+
203
+ render template: "relationships/followed"
204
+
205
+ end
206
+
207
+
208
+
209
+ def followers
210
+
211
+ #@userをフォローしているユーザー
212
+
213
+ @user = User.find(params[:id])
214
+
215
+ @users = @user.followers
216
+
217
+ render template: "relationships/follower"
218
+
219
+ end
220
+
221
+
222
+
223
+ private
224
+
225
+
226
+
227
+ def user_params
228
+
229
+ params.require(:user).permit(:name, :profile, :profile_image)
230
+
231
+ end
232
+
233
+
234
+
235
+ def ensure_correct_user
236
+
237
+ @user = User.find(params[:id])
238
+
239
+ unless @user == current_user
240
+
241
+ redirect_to user_path(current_user)
242
+
243
+ end
244
+
245
+ end
246
+
247
+
122
248
 
123
249
  end
124
250
 
125
251
 
126
252
 
127
-
253
+ _info.html.erb
254
+
128
-
255
+ ```html
256
+
129
-
257
+ コード
258
+
130
-
259
+ ```<table class='table'>
260
+
131
-
261
+ <tr><%= attachment_image_tag user, :profile_image, :fill, 100, 100, fallback: "no-image-icon.jpg", class:"mt-3 rounded-circle"; %></tr>
262
+
132
-
263
+ <tr>
264
+
265
+ <th><%= user.name %></th>
266
+
267
+ </tr>
268
+
269
+ <tr>
270
+
271
+ <th><%= user.profile %></th>
272
+
273
+ </tr>
274
+
275
+ <tr>
276
+
277
+ <th>follows</th>
278
+
279
+ <th class="follower">
280
+
281
+ <%= link_to followers_user_path(user.id) do %>
282
+
283
+ <h5 style="color: black;"><%= user.followers.count %></h5>
284
+
285
+ <% end %>
286
+
287
+ </th>
288
+
289
+ </tr>
290
+
291
+ <tr>
292
+
293
+ <th>followers</th>
294
+
295
+ <th class="follow">
296
+
297
+ <%= link_to followed_user_path(user.id) do %>
298
+
299
+ <h5 style="color: black;"><%= user.followed.count %></h5>
300
+
301
+ <% end %>
302
+
303
+ </div>
304
+
305
+ </tr>
306
+
307
+ <tr>
308
+
309
+ <th>
310
+
311
+ <% if user_signed_in? && @user != current_user %>
312
+
313
+ <div id="follow_form">
314
+
315
+ <% if current_user.followed?(@user) %>
316
+
317
+ <%= render "users/unfollow" %>
318
+
319
+ <% else %>
320
+
321
+ <%= render "users/follow" %>
322
+
323
+ <% end %>
324
+
325
+ </div>
326
+
327
+ <% end %>
328
+
329
+ </th>
330
+
331
+ </tr>
332
+
333
+ </table>
334
+
335
+
336
+
337
+ <div class='row'>
338
+
339
+ <%= link_to '',edit_user_path(user),class: "btn btn-outline-secondary btn-block fas fa-user-cog edit_user_#{user.id}" %>
340
+
341
+ </div>
342
+
343
+ _
344
+
345
+
346
+
347
+ _index.htm.erb
348
+
349
+ ```html
350
+
351
+ コード
352
+
353
+ ```<div class="card-list-containar py-5">
354
+
355
+ <% @articles.each do |article| %>
356
+
357
+ <div class="card w-50 mx-auto mt-4 shadow-lg">
358
+
359
+ <%= link_to article_path(article.id) do %>
360
+
361
+ <%= attachment_image_tag article, :image, class:"card-img-top"; %>
362
+
363
+ <% end %>
364
+
365
+ <div class="card-body">
366
+
367
+ <div class="row">
368
+
369
+ <div class="col-2">
370
+
371
+ <%= attachment_image_tag article.user, :profile_image, :fill, 60, 60, fallback: "no-image-icon.jpg", class:"mt-3 rounded-circle"; %>
372
+
373
+ </div>
374
+
375
+ <div class="col-10">
376
+
377
+ <p class="card-title">
378
+
379
+ <%= link_to(article_path(article.id), class:"text-dark") do %>
380
+
381
+ <strong><i class="far fa-flag"></i><span> <%= article.title %></span></strong>
382
+
383
+ <% end %>
384
+
385
+ </P>
386
+
387
+ <p class="card-text">本文:<%= article.body.truncate(30) %></p>
388
+
389
+ <span><i class="fas fa-user"></i> By <%=link_to article.user.name %> | </span>
390
+
391
+ <span><i class ="favorite_btn_<%= article.id %>"><%= render "favorites/favorite-btn", article: article %></i></span>
392
+
393
+ </div>
394
+
395
+ </div>
396
+
397
+ </div>
398
+
399
+ </div>
400
+
401
+ <% end %>
402
+
403
+ <!--%= paginate post_images %>-->
404
+
405
+ </div>
406
+
407
+
408
+
133
- users_controller.rb
409
+ relationship.rb
134
410
 
135
411
  ```ruby
136
412
 
137
413
  コード
138
414
 
139
- ```class UsersController < ApplicationController
415
+ ```class Relationship < ApplicationRecord
140
-
141
-
142
-
143
- before_action :authenticate_user!
416
+
144
-
145
- before_action :ensure_correct_user, only: [:edit, :update]
417
+
146
-
147
-
148
-
149
- def show
418
+
150
-
151
- @user = User.find(params[:id])
152
-
153
- @articles = @user.articles
154
-
155
- # @article = Article.new
156
-
157
- end
158
-
159
-
160
-
161
- def index
162
-
163
- @users = User.all
419
+ #自分をフォローしているユーザー
164
-
165
- end
420
+
166
-
167
-
168
-
169
- def edit
170
-
171
- @user = User.find(params[:id])
172
-
173
- end
174
-
175
-
176
-
177
- def update
178
-
179
- @user = User.find(params[:id])
180
-
181
- if @user.update(user_params)
182
-
183
- redirect_to user_path(@user), notice: "更新しました!"
421
+ belongs_to :follower, class_name: "User"
184
-
185
- else
422
+
186
-
187
- render "edit"
188
-
189
- end
190
-
191
- end
192
-
193
-
194
-
195
- def followed
196
-
197
- #@userがフォローしているユーザー
423
+ #自分がフォローしているユーザー
198
-
199
- @user = User.find(params[:id])
424
+
200
-
201
- @users = @user.followed
202
-
203
- render template: "relationships/followed"
204
-
205
- end
206
-
207
-
208
-
209
- def followers
210
-
211
- #@userをフォローしているユーザー
212
-
213
- @user = User.find(params[:id])
214
-
215
- @users = @user.followers
216
-
217
- render template: "relationships/follower"
425
+ belongs_to :followed, class_name: "User"
218
-
219
- end
426
+
220
-
221
-
222
-
223
- private
427
+ #バリデーション
224
-
225
-
226
-
227
- def user_params
428
+
228
-
229
- params.require(:user).permit(:name, :profile, :profile_image)
230
-
231
- end
232
-
233
-
234
-
235
- def ensure_correct_user
236
-
237
- @user = User.find(params[:id])
238
-
239
- unless @user == current_user
240
-
241
- redirect_to user_path(current_user)
429
+ validates :follower_id, presence: true
242
-
430
+
243
- end
431
+ validates :followed_id, presence: true
244
-
245
- end
246
432
 
247
433
 
248
434
 
@@ -250,232 +436,46 @@
250
436
 
251
437
 
252
438
 
253
- _info.html.erb
439
+ relationships_controller.rb
254
-
440
+
255
- ```html
441
+ ```ruby
256
442
 
257
443
  コード
258
444
 
259
- ```<table class='table'>
260
-
261
- <tr><%= attachment_image_tag user, :profile_image, :fill, 100, 100, fallback: "no-image-icon.jpg", class:"mt-3 rounded-circle"; %></tr>
262
-
263
- <tr>
264
-
265
- <th><%= user.name %></th>
266
-
267
- </tr>
268
-
269
- <tr>
270
-
271
- <th><%= user.profile %></th>
272
-
273
- </tr>
274
-
275
- <tr>
276
-
277
- <th>follows</th>
278
-
279
- <th class="follower">
280
-
281
- <%= link_to followers_user_path(user.id) do %>
282
-
283
- <h5 style="color: black;"><%= user.followers.count %></h5>
284
-
285
- <% end %>
286
-
287
- </th>
288
-
289
- </tr>
290
-
291
- <tr>
292
-
293
- <th>followers</th>
294
-
295
- <th class="follow">
296
-
297
- <%= link_to followed_user_path(user.id) do %>
298
-
299
- <h5 style="color: black;"><%= user.followed.count %></h5>
300
-
301
- <% end %>
302
-
303
- </div>
304
-
305
- </tr>
306
-
307
- <tr>
308
-
309
- <th>
310
-
311
- <% if user_signed_in? && @user != current_user %>
312
-
313
- <div id="follow_form">
314
-
315
- <% if current_user.followed?(@user) %>
316
-
317
- <%= render "users/unfollow" %>
318
-
319
- <% else %>
320
-
321
- <%= render "users/follow" %>
322
-
323
- <% end %>
324
-
325
- </div>
326
-
327
- <% end %>
328
-
329
- </th>
330
-
331
- </tr>
332
-
333
- </table>
334
-
335
-
336
-
337
- <div class='row'>
338
-
339
- <%= link_to '',edit_user_path(user),class: "btn btn-outline-secondary btn-block fas fa-user-cog edit_user_#{user.id}" %>
340
-
341
- </div>
342
-
343
- _
344
-
345
-
346
-
347
- _index.htm.erb
348
-
349
- ```html
350
-
351
- コード
352
-
353
- ```<div class="card-list-containar py-5">
354
-
355
- <% @articles.each do |article| %>
356
-
357
- <div class="card w-50 mx-auto mt-4 shadow-lg">
358
-
359
- <%= link_to article_path(article.id) do %>
360
-
361
- <%= attachment_image_tag article, :image, class:"card-img-top"; %>
362
-
363
- <% end %>
364
-
365
- <div class="card-body">
366
-
367
- <div class="row">
368
-
369
- <div class="col-2">
370
-
371
- <%= attachment_image_tag article.user, :profile_image, :fill, 60, 60, fallback: "no-image-icon.jpg", class:"mt-3 rounded-circle"; %>
372
-
373
- </div>
374
-
375
- <div class="col-10">
376
-
377
- <p class="card-title">
378
-
379
- <%= link_to(article_path(article.id), class:"text-dark") do %>
380
-
381
- <strong><i class="far fa-flag"></i><span> <%= article.title %></span></strong>
382
-
383
- <% end %>
384
-
385
- </P>
386
-
387
- <p class="card-text">本文:<%= article.body.truncate(30) %></p>
388
-
389
- <span><i class="fas fa-user"></i> By <%=link_to article.user.name %> | </span>
390
-
391
- <span><i class ="favorite_btn_<%= article.id %>"><%= render "favorites/favorite-btn", article: article %></i></span>
392
-
393
- </div>
394
-
395
- </div>
396
-
397
- </div>
398
-
399
- </div>
400
-
401
- <% end %>
402
-
403
- <!--%= paginate post_images %>-->
404
-
405
- </div>
406
-
407
-
408
-
409
- relationship.rb
410
-
411
- ```ruby
412
-
413
- コード
414
-
415
- ```class Relationship < ApplicationRecord
416
-
417
-
418
-
419
- #自分をフォローしているユーザー
420
-
421
- belongs_to :follower, class_name: "User"
422
-
423
- #自分がフォローしているユーザー
424
-
425
- belongs_to :followed, class_name: "User"
426
-
427
- #バリデーション
428
-
429
- validates :follower_id, presence: true
430
-
431
- validates :followed_id, presence: true
432
-
433
-
445
+ ```class RelationshipsController < ApplicationController
446
+
447
+
448
+
449
+
450
+
451
+ # フォローする
452
+
453
+ def create
454
+
455
+ @user = User.find(params[:followed_id])
456
+
457
+ current_user.follow(@user)
458
+
459
+ end
460
+
461
+
462
+
463
+ # アンフォローする
464
+
465
+ def destroy
466
+
467
+ @user = User.find(params[:id])
468
+
469
+ current_user.unfollow(@user)
470
+
471
+ end
472
+
473
+
434
474
 
435
475
  end
436
476
 
437
477
 
438
478
 
439
- relationships_controller.rb
440
-
441
- ```ruby
442
-
443
- コード
444
-
445
- ```class RelationshipsController < ApplicationController
446
-
447
-
448
-
449
-
450
-
451
- # フォローする
452
-
453
- def create
454
-
455
- @user = User.find(params[:followed_id])
456
-
457
- current_user.follow(@user)
458
-
459
- end
460
-
461
-
462
-
463
- # アンフォローする
464
-
465
- def destroy
466
-
467
- @user = User.find(params[:id])
468
-
469
- current_user.unfollow(@user)
470
-
471
- end
472
-
473
-
474
-
475
- end
476
-
477
-
478
-
479
479
  ### 試したこと
480
480
 
481
481
  フォローの非同期化は以下の記事を参考に作成しております。

4

各コードを画像からコード挿入に変更しました!

2021/07/23 08:42

投稿

dodekamin.taro
dodekamin.taro

スコア0

test CHANGED
File without changes
test CHANGED
@@ -52,9 +52,7 @@
52
52
 
53
53
  コード
54
54
 
55
- ```
56
-
57
- class User < ApplicationRecord
55
+ ```class User < ApplicationRecord
58
56
 
59
57
  # Include default devise modules. Others available are:
60
58
 
@@ -138,9 +136,7 @@
138
136
 
139
137
  コード
140
138
 
141
- ```
142
-
143
- class UsersController < ApplicationController
139
+ ```class UsersController < ApplicationController
144
140
 
145
141
 
146
142
 
@@ -260,9 +256,7 @@
260
256
 
261
257
  コード
262
258
 
263
- ```
264
-
265
- <table class='table'>
259
+ ```<table class='table'>
266
260
 
267
261
  <tr><%= attachment_image_tag user, :profile_image, :fill, 100, 100, fallback: "no-image-icon.jpg", class:"mt-3 rounded-circle"; %></tr>
268
262
 
@@ -356,9 +350,7 @@
356
350
 
357
351
  コード
358
352
 
359
- ```
360
-
361
- <div class="card-list-containar py-5">
353
+ ```<div class="card-list-containar py-5">
362
354
 
363
355
  <% @articles.each do |article| %>
364
356
 
@@ -420,9 +412,7 @@
420
412
 
421
413
  コード
422
414
 
423
- ```
424
-
425
- class Relationship < ApplicationRecord
415
+ ```class Relationship < ApplicationRecord
426
416
 
427
417
 
428
418
 
@@ -452,9 +442,7 @@
452
442
 
453
443
  コード
454
444
 
455
- ```
456
-
457
- class RelationshipsController < ApplicationController
445
+ ```class RelationshipsController < ApplicationController
458
446
 
459
447
 
460
448
 

3

各コードを画像からコード挿入に変更しました。

2021/07/23 08:40

投稿

dodekamin.taro
dodekamin.taro

スコア0

test CHANGED
File without changes
test CHANGED
@@ -48,6 +48,12 @@
48
48
 
49
49
  user.rb
50
50
 
51
+ ```ruby
52
+
53
+ コード
54
+
55
+ ```
56
+
51
57
  class User < ApplicationRecord
52
58
 
53
59
  # Include default devise modules. Others available are:
@@ -120,9 +126,19 @@
120
126
 
121
127
 
122
128
 
129
+
130
+
131
+
132
+
133
+
134
+
123
135
  users_controller.rb
124
136
 
125
-
137
+ ```ruby
138
+
139
+ コード
140
+
141
+ ```
126
142
 
127
143
  class UsersController < ApplicationController
128
144
 
@@ -240,6 +256,12 @@
240
256
 
241
257
  _info.html.erb
242
258
 
259
+ ```html
260
+
261
+ コード
262
+
263
+ ```
264
+
243
265
  <table class='table'>
244
266
 
245
267
  <tr><%= attachment_image_tag user, :profile_image, :fill, 100, 100, fallback: "no-image-icon.jpg", class:"mt-3 rounded-circle"; %></tr>
@@ -326,8 +348,16 @@
326
348
 
327
349
  _
328
350
 
351
+
352
+
329
353
  _index.htm.erb
330
354
 
355
+ ```html
356
+
357
+ コード
358
+
359
+ ```
360
+
331
361
  <div class="card-list-containar py-5">
332
362
 
333
363
  <% @articles.each do |article| %>
@@ -386,6 +416,12 @@
386
416
 
387
417
  relationship.rb
388
418
 
419
+ ```ruby
420
+
421
+ コード
422
+
423
+ ```
424
+
389
425
  class Relationship < ApplicationRecord
390
426
 
391
427
 
@@ -412,6 +448,12 @@
412
448
 
413
449
  relationships_controller.rb
414
450
 
451
+ ```ruby
452
+
453
+ コード
454
+
455
+ ```
456
+
415
457
  class RelationshipsController < ApplicationController
416
458
 
417
459
 

2

各コードを画像からコード直打ちに変更しました。

2021/07/23 08:39

投稿

dodekamin.taro
dodekamin.taro

スコア0

test CHANGED
File without changes
test CHANGED
@@ -48,39 +48,401 @@
48
48
 
49
49
  user.rb
50
50
 
51
+ class User < ApplicationRecord
52
+
53
+ # Include default devise modules. Others available are:
54
+
55
+ # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
56
+
51
- ![イメージ説明](8eef886a9ef44c01e4861a66a1d2cc60.png)
57
+ devise :database_authenticatable, :registerable,
58
+
59
+ :recoverable, :rememberable, :validatable
60
+
61
+
62
+
63
+ has_many :articles
64
+
65
+ has_many :favorites, dependent: :destroy
66
+
67
+ attachment :profile_image, destroy: false
68
+
69
+
70
+
71
+ validates :name, length: { minimum: 2, maximum: 20 }, uniqueness: true
72
+
73
+ validates :profile, length: { maximum: 200 }
74
+
75
+
76
+
77
+ has_many :followed_relationships, foreign_key: "follower_id", class_name: "Relationship", dependent: :destroy
78
+
79
+ has_many :followed, through: :followed_relationships
80
+
81
+ has_many :follower_relationships, foreign_key: "followed_id", class_name: "Relationship", dependent: :destroy
82
+
83
+ has_many :followers, through: :follower_relationships
84
+
85
+
86
+
87
+ #フォローしているかを確認するメソッド
88
+
89
+ def followed?(user)
90
+
91
+ followed_relationships.find_by(followed_id: user.id)
92
+
93
+ end
94
+
95
+
96
+
97
+ #フォローするときのメソッド
98
+
99
+ def follow(user)
100
+
101
+ followed_relationships.create!(followed_id: user.id)
102
+
103
+ end
104
+
105
+
106
+
107
+ #フォローを外すときのメソッド
108
+
109
+ def unfollow(user)
110
+
111
+ followed_relationships.find_by(followed_id: user.id).destroy
112
+
113
+ end
114
+
115
+
116
+
117
+
118
+
119
+ end
52
120
 
53
121
 
54
122
 
55
123
  users_controller.rb
56
124
 
125
+
126
+
57
- ![イメージ説明](04bfacf3308ff1db9c68cbfe50753d09.png)
127
+ class UsersController < ApplicationController
128
+
129
+
130
+
58
-
131
+ before_action :authenticate_user!
132
+
59
- ![イメージ説明](26f21ec8a45cbc8de7bf6d8c4d01a698.png)
133
+ before_action :ensure_correct_user, only: [:edit, :update]
134
+
135
+
136
+
137
+ def show
138
+
139
+ @user = User.find(params[:id])
140
+
141
+ @articles = @user.articles
142
+
143
+ # @article = Article.new
144
+
145
+ end
146
+
147
+
148
+
149
+ def index
150
+
151
+ @users = User.all
152
+
153
+ end
154
+
155
+
156
+
157
+ def edit
158
+
159
+ @user = User.find(params[:id])
160
+
161
+ end
162
+
163
+
164
+
165
+ def update
166
+
167
+ @user = User.find(params[:id])
168
+
169
+ if @user.update(user_params)
170
+
171
+ redirect_to user_path(@user), notice: "更新しました!"
172
+
173
+ else
174
+
175
+ render "edit"
176
+
177
+ end
178
+
179
+ end
180
+
181
+
182
+
183
+ def followed
184
+
185
+ #@userがフォローしているユーザー
186
+
187
+ @user = User.find(params[:id])
188
+
189
+ @users = @user.followed
190
+
191
+ render template: "relationships/followed"
192
+
193
+ end
194
+
195
+
196
+
197
+ def followers
198
+
199
+ #@userをフォローしているユーザー
200
+
201
+ @user = User.find(params[:id])
202
+
203
+ @users = @user.followers
204
+
205
+ render template: "relationships/follower"
206
+
207
+ end
208
+
209
+
210
+
211
+ private
212
+
213
+
214
+
215
+ def user_params
216
+
217
+ params.require(:user).permit(:name, :profile, :profile_image)
218
+
219
+ end
220
+
221
+
222
+
223
+ def ensure_correct_user
224
+
225
+ @user = User.find(params[:id])
226
+
227
+ unless @user == current_user
228
+
229
+ redirect_to user_path(current_user)
230
+
231
+ end
232
+
233
+ end
234
+
235
+
236
+
237
+ end
60
238
 
61
239
 
62
240
 
63
241
  _info.html.erb
64
242
 
243
+ <table class='table'>
244
+
245
+ <tr><%= attachment_image_tag user, :profile_image, :fill, 100, 100, fallback: "no-image-icon.jpg", class:"mt-3 rounded-circle"; %></tr>
246
+
247
+ <tr>
248
+
249
+ <th><%= user.name %></th>
250
+
251
+ </tr>
252
+
253
+ <tr>
254
+
255
+ <th><%= user.profile %></th>
256
+
257
+ </tr>
258
+
259
+ <tr>
260
+
261
+ <th>follows</th>
262
+
263
+ <th class="follower">
264
+
265
+ <%= link_to followers_user_path(user.id) do %>
266
+
267
+ <h5 style="color: black;"><%= user.followers.count %></h5>
268
+
269
+ <% end %>
270
+
271
+ </th>
272
+
273
+ </tr>
274
+
275
+ <tr>
276
+
277
+ <th>followers</th>
278
+
279
+ <th class="follow">
280
+
281
+ <%= link_to followed_user_path(user.id) do %>
282
+
283
+ <h5 style="color: black;"><%= user.followed.count %></h5>
284
+
285
+ <% end %>
286
+
287
+ </div>
288
+
289
+ </tr>
290
+
291
+ <tr>
292
+
293
+ <th>
294
+
295
+ <% if user_signed_in? && @user != current_user %>
296
+
297
+ <div id="follow_form">
298
+
299
+ <% if current_user.followed?(@user) %>
300
+
301
+ <%= render "users/unfollow" %>
302
+
303
+ <% else %>
304
+
65
- ![イメージ説明](c83d3bb60a87514c1b2114c9d4bd7415.png)
305
+ <%= render "users/follow" %>
306
+
307
+ <% end %>
308
+
309
+ </div>
310
+
311
+ <% end %>
312
+
313
+ </th>
314
+
315
+ </tr>
316
+
317
+ </table>
318
+
319
+
320
+
321
+ <div class='row'>
322
+
323
+ <%= link_to '',edit_user_path(user),class: "btn btn-outline-secondary btn-block fas fa-user-cog edit_user_#{user.id}" %>
324
+
325
+ </div>
66
326
 
67
327
  _
68
328
 
69
329
  _index.htm.erb
70
330
 
331
+ <div class="card-list-containar py-5">
332
+
333
+ <% @articles.each do |article| %>
334
+
335
+ <div class="card w-50 mx-auto mt-4 shadow-lg">
336
+
71
- ![イメージ説明](4ab5345d5ba0667d294adf4f879a30e0.png)
337
+ <%= link_to article_path(article.id) do %>
338
+
339
+ <%= attachment_image_tag article, :image, class:"card-img-top"; %>
340
+
341
+ <% end %>
342
+
343
+ <div class="card-body">
344
+
345
+ <div class="row">
346
+
347
+ <div class="col-2">
348
+
349
+ <%= attachment_image_tag article.user, :profile_image, :fill, 60, 60, fallback: "no-image-icon.jpg", class:"mt-3 rounded-circle"; %>
350
+
351
+ </div>
352
+
353
+ <div class="col-10">
354
+
355
+ <p class="card-title">
356
+
357
+ <%= link_to(article_path(article.id), class:"text-dark") do %>
358
+
359
+ <strong><i class="far fa-flag"></i><span> <%= article.title %></span></strong>
360
+
361
+ <% end %>
362
+
363
+ </P>
364
+
365
+ <p class="card-text">本文:<%= article.body.truncate(30) %></p>
366
+
367
+ <span><i class="fas fa-user"></i> By <%=link_to article.user.name %> | </span>
368
+
369
+ <span><i class ="favorite_btn_<%= article.id %>"><%= render "favorites/favorite-btn", article: article %></i></span>
370
+
371
+ </div>
372
+
373
+ </div>
374
+
375
+ </div>
376
+
377
+ </div>
378
+
379
+ <% end %>
380
+
381
+ <!--%= paginate post_images %>-->
382
+
383
+ </div>
72
384
 
73
385
 
74
386
 
75
387
  relationship.rb
76
388
 
389
+ class Relationship < ApplicationRecord
390
+
391
+
392
+
393
+ #自分をフォローしているユーザー
394
+
77
- ![イメージ説明](bcb56d285f07b59c262ef6b470512bd1.png)
395
+ belongs_to :follower, class_name: "User"
396
+
397
+ #自分がフォローしているユーザー
398
+
399
+ belongs_to :followed, class_name: "User"
400
+
401
+ #バリデーション
402
+
403
+ validates :follower_id, presence: true
404
+
405
+ validates :followed_id, presence: true
406
+
407
+
408
+
409
+ end
78
410
 
79
411
 
80
412
 
81
413
  relationships_controller.rb
82
414
 
415
+ class RelationshipsController < ApplicationController
416
+
417
+
418
+
419
+
420
+
421
+ # フォローする
422
+
423
+ def create
424
+
83
- ![イメージ説明](5c462ae7cedb2f93254fd87dbf560bc5.png)
425
+ @user = User.find(params[:followed_id])
426
+
427
+ current_user.follow(@user)
428
+
429
+ end
430
+
431
+
432
+
433
+ # アンフォローする
434
+
435
+ def destroy
436
+
437
+ @user = User.find(params[:id])
438
+
439
+ current_user.unfollow(@user)
440
+
441
+ end
442
+
443
+
444
+
445
+ end
84
446
 
85
447
 
86
448
 

1

relationshipコントロラー、モデルのソースを追加しました。

2021/07/23 08:34

投稿

dodekamin.taro
dodekamin.taro

スコア0

test CHANGED
File without changes
test CHANGED
@@ -72,6 +72,16 @@
72
72
 
73
73
 
74
74
 
75
+ relationship.rb
76
+
77
+ ![イメージ説明](bcb56d285f07b59c262ef6b470512bd1.png)
78
+
79
+
80
+
81
+ relationships_controller.rb
82
+
83
+ ![イメージ説明](5c462ae7cedb2f93254fd87dbf560bc5.png)
84
+
75
85
 
76
86
 
77
87
  ### 試したこと