質問編集履歴
1
質問内容がしっかり書かれていなかったため修正しました。
title
CHANGED
File without changes
|
body
CHANGED
@@ -11,13 +11,86 @@
|
|
11
11
|
### 該当のソースコード
|
12
12
|
|
13
13
|
```routes
|
14
|
-
|
14
|
+
get 'users/:id' =>"users#show"
|
15
|
+
|
16
|
+
post 'relationship/:id/unfollow' => 'relationships#unfollow',as: 'unfollow'
|
17
|
+
post 'relationship/:id/follow' => 'relationships#follow', as: 'follow'
|
15
18
|
```
|
16
19
|
|
20
|
+
```userrb
|
21
|
+
class User < ApplicationRecord
|
22
|
+
validates :password, {presence: true, length: {minimum: 8}, uniqueness: true}
|
23
|
+
validates :user_name, {presence: true}
|
24
|
+
validates :email, {presence: true}
|
25
|
+
|
26
|
+
has_many :follow, class_name:"Relationship", foreign_key:"follow_id"
|
27
|
+
has_many :follower, class_name:"Relationship", foreign_key:"follower_id"
|
28
|
+
|
29
|
+
has_many :following_user, through: :follow, source: :follower
|
30
|
+
has_many :follower_user, through: :follower, source: :follow
|
31
|
+
|
32
|
+
def follow(user_id)
|
33
|
+
follow.create(follower_id: user_id)
|
34
|
+
end
|
35
|
+
|
36
|
+
|
37
|
+
def unfollow(user_id)
|
38
|
+
follow.find_by(follower_id: user_id).destroy
|
39
|
+
end
|
40
|
+
|
41
|
+
|
42
|
+
def following?(user)
|
43
|
+
following_user.include?(user)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
```
|
47
|
+
|
48
|
+
```relationshiprb
|
49
|
+
class Relationship < ApplicationRecord
|
50
|
+
belongs_to :follow, class_name: "User"
|
51
|
+
belongs_to :follower, class_name: "User"
|
52
|
+
end
|
53
|
+
```
|
54
|
+
|
55
|
+
```userscontroller
|
56
|
+
def show
|
57
|
+
@user =User.find_by(id: params[:id])
|
58
|
+
end
|
59
|
+
```
|
60
|
+
|
61
|
+
```relationshipscontroller
|
62
|
+
class RelationshipsController < ApplicationController
|
63
|
+
|
64
|
+
def follow
|
65
|
+
@user=User.find_by(id: params[:id])
|
66
|
+
@current_user.follow(params[:id])
|
67
|
+
reidirect_to("/user/#{@user.id}")
|
68
|
+
end
|
69
|
+
|
70
|
+
end
|
71
|
+
```
|
72
|
+
|
73
|
+
```showhtml
|
74
|
+
<% if @user.id == @current_user.id %>
|
75
|
+
<%= link_to("/users/#{@user.id}/edit") do %>
|
76
|
+
<div class="update-wrapper">プロフィールを編集する</div>
|
77
|
+
<% end %>
|
78
|
+
<% elssif @current_user.following?(@user)%>
|
79
|
+
<%= link_to(unfollow_path(@user.id), {method: "post"}) do %>
|
80
|
+
<div class="update-wrapper">フォロー中</div>
|
81
|
+
<% end %>
|
82
|
+
<% else %>
|
83
|
+
<%= link_to(follow_path(@user.id), {method: "post"}) do %>
|
84
|
+
<div class="update-wrapper">フォローする</div>
|
85
|
+
<% end %>
|
86
|
+
<% end %>
|
87
|
+
```
|
88
|
+
変数はrelationshipscontrollerの```@current_user.follow(params[:id])```のparams[:id]として渡せていると思っていたのですが何が間違えているのか自分ではわかりません。申し訳ありませんが皆さんの力をお貸しいただければと思います。
|
89
|
+
よろしくお願いします。
|
17
90
|
### 試したこと
|
91
|
+
他の方の記事も参考にrelationshipモデルのカラムを変更したりいろいろ試しましたが自分でもあまり理解できず、理解できそうな上記の方の記事を頑張って参考に作成してみようと思いました。
|
18
92
|
|
19
|
-
ここに問題に対して試したことを記載してください。
|
20
|
-
|
21
93
|
### 補足情報(FW/ツールのバージョンなど)
|
22
94
|
|
95
|
+
rails5を用いています。
|
23
|
-
|
96
|
+
足りない情報がありましたら追記いたします
|