質問編集履歴
8
あ
title
CHANGED
File without changes
|
body
CHANGED
@@ -1,255 +1,1 @@
|
|
1
|
-
現在、下記記事を参考にしてフォロー機能を実装しています。
|
2
|
-
https://qiita.com/mitsumitsu1128/items/e41e2ff37f143db81897#relationships%E3%82%B3%E3%83%B3%E3%83%88%E3%83%AD%E3%83%BC%E3%83%A9%E3%82%92%E4%BD%9C%E6%88%90%E7%B7%A8%E9%9B%86
|
3
|
-
|
4
|
-
下記のエラーが表示されています。
|
5
|
-

|
6
|
-
|
7
|
-
##【推測】
|
8
|
-
・user_idの値を渡すはずなのにgym_idの値を渡してしまっている。
|
9
|
-
|
10
|
-
|
11
|
-
【試したこと】
|
12
|
-
gym_idが1の投稿にいくと、ボタンが表示されるようです。
|
13
|
-
|
14
|
-

|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
記事中では、<%= render 'relationships/follow', user: @user %>を好きなところにおけば良いということでしたが動きませんでした。
|
22
|
-
|
23
|
-
|
24
|
-
##コード
|
25
|
-
|
26
|
-
フォローボタンを配置したい場所はgymコントローラのshowアクションになります。
|
27
|
-
views/gyms/show.html.erb
|
28
|
-
|
29
|
-
```
|
30
|
-
<% set_meta_tags title: @gym.name %>
|
31
|
-
<% breadcrumb :gym, @gym %>
|
32
|
-
|
33
|
-
<div class="sidebar">
|
34
|
-
|
1
|
+
あああああああああああああああああああああああああああああああああああああああああああ
|
35
|
-
<h4 class ="recent_post">人気記事</h4>
|
36
|
-
<ul class="popular-posts">
|
37
|
-
<li class="sidebar_row">
|
38
|
-
<% @related_blogs.each do |blog| %>
|
39
|
-
<div class="sidebar_blogbox">
|
40
|
-
<div class="gym_sidebar_title clearfix">
|
41
|
-
<%= link_to (blog.title), blog_path(blog) %>
|
42
|
-
</div>
|
43
|
-
<% end %>
|
44
|
-
</div>
|
45
|
-
</li>
|
46
|
-
<h4 class ="recent_gym_post">投稿</h4>
|
47
|
-
<ul class="gym-posts">
|
48
|
-
<li class="sidebar_row">
|
49
|
-
<% @related_gyms.each do |related_gym| %>
|
50
|
-
<div class="sidebar_related_gymbox">
|
51
|
-
<div class ="related_gym_picture"><%= image_tag(related_gym.picture.first.url) %></div>
|
52
|
-
<div class ="related_gym_name"><%= link_to (related_gym.name),gym_path(related_gym) %></div>
|
53
|
-
</div>
|
54
|
-
<% end %>
|
55
|
-
</li>
|
56
|
-
</ul>
|
57
|
-
</div>
|
58
|
-
</div>
|
59
|
-
<div class="show-gymbox">
|
60
|
-
<h2 class ="gym_name"><%= @gym.name %></h2>
|
61
|
-
<div class="gym_picture">
|
62
|
-
<div class="row">
|
63
|
-
<% if @gym.picture[0].present? %>
|
64
|
-
<div class="col-6">
|
65
|
-
<%= image_tag "#{@gym.picture[0].url}", :alt => @gym.name %>
|
66
|
-
<% end %>
|
67
|
-
</div>
|
68
|
-
<% if @gym.picture[0].present? %>
|
69
|
-
<div class="col-4">
|
70
|
-
<% @gym.picture.each_with_index do |picture,index| %>
|
71
|
-
<%= image_tag "#{@gym.picture[index].url}", :alt => @gym.name %>
|
72
|
-
<% end %>
|
73
|
-
<% end %>
|
74
|
-
</div>
|
75
|
-
</div>
|
76
|
-
</div>
|
77
|
-
<h4 class ="ctitle"> <%= @gym.content %></h4>
|
78
|
-
<% if current_user?(@gym.user) %>
|
79
|
-
<button type="button" class="btn btn-success">
|
80
|
-
<%= link_to "削除", @gym, method: :delete,data: { confirm: "削除しますか?" } %>
|
81
|
-
<% end %>
|
82
|
-
</button>
|
83
|
-
<button type="button" class="btn btn-success">
|
84
|
-
<%= link_to '戻る', gyms_path %>
|
85
|
-
</button>
|
86
|
-
|
87
|
-
<div class ="gym-like">
|
88
|
-
<a class ="gym-like-a">いいね:<%= @gym.likes.count %></a>
|
89
|
-
<% if current_user.already_liked?(@gym) %>
|
90
|
-
<div class ="gym-like-button">
|
91
|
-
<%= link_to '取り消す', gym_like_path(@gym), method: :delete %>
|
92
|
-
</div>
|
93
|
-
<% else %>
|
94
|
-
<div class ="gym-like-button">
|
95
|
-
<%= link_to 'いいね', gym_likes_path(@gym),method: :post %>
|
96
|
-
</div>
|
97
|
-
<% end %>
|
98
|
-
</div>
|
99
|
-
|
100
|
-
<h5 class ="ctitle"><%= render partial:'comments/comment_form' %></h5>
|
101
|
-
<%= render 'relationships/follow', user: @user %> #ここを追加しています
|
102
|
-
<p>
|
103
|
-
<button type="button" class="btn btn-primary" data-toggle="collapse" data-target="#collapseExample" aria-expanded="false" aria-controls="collapseExample">
|
104
|
-
コメントを表示
|
105
|
-
</button>
|
106
|
-
</p>
|
107
|
-
</div>
|
108
|
-
<div class="collapse" id="collapseExample">
|
109
|
-
<div class="card card-body">
|
110
|
-
<div class="comment">
|
111
|
-
<% if @comments.any? %>
|
112
|
-
<%= render @comments %>
|
113
|
-
<%= will_paginate @comments %>
|
114
|
-
<% end %>
|
115
|
-
</div>
|
116
|
-
</div>
|
117
|
-
</div>
|
118
|
-
```
|
119
|
-
|
120
|
-
|
121
|
-
コードは下記の通りです。
|
122
|
-
|
123
|
-
app/models/user.rb
|
124
|
-
|
125
|
-
```
|
126
|
-
class User < ApplicationRecord
|
127
|
-
has_many :relationships
|
128
|
-
has_many :followings, through: :relationships, source: :follow
|
129
|
-
has_many :reverse_of_relationships, class_name: 'Relationship', foreign_key: 'follow_id'
|
130
|
-
has_many :followers, through: :reverse_of_relationships, source: :user
|
131
|
-
|
132
|
-
def follow(other_user)
|
133
|
-
unless self == other_user
|
134
|
-
self.relationships.find_or_create_by(follow_id: other_user.id)
|
135
|
-
end
|
136
|
-
end
|
137
|
-
|
138
|
-
def unfollow(other_user)
|
139
|
-
relationship = self.relationships.find_by(follow_id: other_user.id)
|
140
|
-
relationship.destroy if relationship
|
141
|
-
end
|
142
|
-
|
143
|
-
def following?(other_user)
|
144
|
-
self.followings.include?(other_user)
|
145
|
-
end
|
146
|
-
|
147
|
-
end
|
148
|
-
```
|
149
|
-
|
150
|
-
|
151
|
-
app/controllers/relationships_controller.rb
|
152
|
-
|
153
|
-
```
|
154
|
-
class RelationshipsController < ApplicationController
|
155
|
-
before_action :set_user
|
156
|
-
|
157
|
-
def create
|
158
|
-
following = current_user.follow(@user)
|
159
|
-
if following.save
|
160
|
-
flash[:success] = 'ユーザーをフォローしました'
|
161
|
-
redirect_to @user
|
162
|
-
else
|
163
|
-
flash.now[:alert] = 'ユーザーのフォローに失敗しました'
|
164
|
-
redirect_to @user
|
165
|
-
end
|
166
|
-
end
|
167
|
-
|
168
|
-
def destroy
|
169
|
-
following = current_user.unfollow(@user)
|
170
|
-
if following.destroy
|
171
|
-
flash[:success] = 'ユーザーのフォローを解除しました'
|
172
|
-
redirect_to @user
|
173
|
-
else
|
174
|
-
flash.now[:alert] = 'ユーザーのフォロー解除に失敗しました'
|
175
|
-
redirect_to @user
|
176
|
-
end
|
177
|
-
end
|
178
|
-
|
179
|
-
private
|
180
|
-
def
|
181
|
-
@user = User.find(params[:relationship][:follow_id])
|
182
|
-
end
|
183
|
-
|
184
|
-
end
|
185
|
-
|
186
|
-
```
|
187
|
-
|
188
|
-
app/views/relationships/_follow_button.html.erb
|
189
|
-
|
190
|
-
```
|
191
|
-
<% unless current_user == user %>
|
192
|
-
<% if current_user.following?(user) %>
|
193
|
-
<%= form_for(current_user.relationships.find_by(follow_id: user.id), html: { method: :delete }) do |f| %>
|
194
|
-
<%= hidden_field_tag :follow_id, user.id %>
|
195
|
-
<%= f.submit 'Unfollow', class: 'btn btn-danger btn-block' %>
|
196
|
-
<% end %>
|
197
|
-
<% else %>
|
198
|
-
<%= form_for(current_user.relationships.build) do |f| %>
|
199
|
-
<%= hidden_field_tag :follow_id, user.id %>
|
200
|
-
<%= f.submit 'Follow', class: 'btn btn-primary btn-block' %>
|
201
|
-
<% end %>
|
202
|
-
<% end %>
|
203
|
-
<% end %>
|
204
|
-
```
|
205
|
-
|
206
|
-
config/routes.rb
|
207
|
-
|
208
|
-
```
|
209
|
-
Rails.application.routes.draw do
|
210
|
-
|
211
|
-
get 'contacts/new'
|
212
|
-
get 'contacts/create'
|
213
|
-
mount Ckeditor::Engine => '/ckeditor'
|
214
|
-
mount RailsAdmin::Engine => '/admin', as: 'rails_admin'
|
215
|
-
|
216
|
-
root 'static_pages#home'
|
217
|
-
#get '/help', to: 'static_pages#help'
|
218
|
-
#get '/about', to: 'static_pages#about'
|
219
|
-
#get '/contact', to: 'static_pages#contact'
|
220
|
-
get '/signup', to: 'users#new'
|
221
|
-
get '/login', to: 'sessions#new'
|
222
|
-
post '/login', to: 'sessions#create'
|
223
|
-
delete '/logout', to: 'sessions#destroy'
|
224
|
-
|
225
|
-
|
226
|
-
resources :users
|
227
|
-
|
228
|
-
resources :account_activations, only: [:edit]
|
229
|
-
|
230
|
-
resources :posts, only: [:new, :show, :create, :destroy]
|
231
|
-
|
232
|
-
resources :comments, only: [:create, :destroy]
|
233
|
-
|
234
|
-
resources :password_resets, only: [:new, :create, :edit, :update]
|
235
|
-
|
236
|
-
resources :gyms do
|
237
|
-
resources :likes, only: [:create, :destroy]
|
238
|
-
end
|
239
|
-
|
240
|
-
resources :blogs
|
241
|
-
|
242
|
-
resources :relationships, only: [:create, :destroy]
|
243
|
-
|
244
|
-
resources :contacts, only: %i(new create) do
|
245
|
-
collection do
|
246
|
-
post :new, path: :new, as: :new, action: :back
|
247
|
-
post :confirm
|
248
|
-
end
|
249
|
-
end
|
250
|
-
end
|
251
|
-
|
252
|
-
```
|
253
|
-
|
254
|
-
|
255
|
-
アドバイス、解決法がありましたらお願いします。
|
7
xz
title
CHANGED
File without changes
|
body
CHANGED
@@ -8,11 +8,16 @@
|
|
8
8
|
・user_idの値を渡すはずなのにgym_idの値を渡してしまっている。
|
9
9
|
|
10
10
|
|
11
|
+
【試したこと】
|
12
|
+
gym_idが1の投稿にいくと、ボタンが表示されるようです。
|
11
13
|
|
14
|
+

|
12
15
|
|
13
16
|
|
14
17
|
|
15
18
|
|
19
|
+
|
20
|
+
|
16
21
|
記事中では、<%= render 'relationships/follow', user: @user %>を好きなところにおけば良いということでしたが動きませんでした。
|
17
22
|
|
18
23
|
|
6
あ
title
CHANGED
@@ -1,1 +1,1 @@
|
|
1
|
-
フォロー機能を
|
1
|
+
フォロー機能に置いて@user = User.find(params[:id]の値をuser_idの値にしたい。
|
body
CHANGED
@@ -2,15 +2,17 @@
|
|
2
2
|
https://qiita.com/mitsumitsu1128/items/e41e2ff37f143db81897#relationships%E3%82%B3%E3%83%B3%E3%83%88%E3%83%AD%E3%83%BC%E3%83%A9%E3%82%92%E4%BD%9C%E6%88%90%E7%B7%A8%E9%9B%86
|
3
3
|
|
4
4
|
下記のエラーが表示されています。
|
5
|
-

|
6
6
|
|
7
7
|
##【推測】
|
8
|
-
・user_idの値
|
8
|
+
・user_idの値を渡すはずなのにgym_idの値を渡してしまっている。
|
9
9
|
|
10
|
-
・gym_idである2の値は渡せている。
|
11
10
|
|
12
11
|
|
13
12
|
|
13
|
+
|
14
|
+
|
15
|
+
|
14
16
|
記事中では、<%= render 'relationships/follow', user: @user %>を好きなところにおけば良いということでしたが動きませんでした。
|
15
17
|
|
16
18
|
|
5
a
title
CHANGED
File without changes
|
body
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
https://qiita.com/mitsumitsu1128/items/e41e2ff37f143db81897#relationships%E3%82%B3%E3%83%B3%E3%83%88%E3%83%AD%E3%83%BC%E3%83%A9%E3%82%92%E4%BD%9C%E6%88%90%E7%B7%A8%E9%9B%86
|
3
3
|
|
4
4
|
下記のエラーが表示されています。
|
5
|
-

|
6
6
|
|
7
7
|
##【推測】
|
8
8
|
・user_idの値が渡せていない。
|
4
s
title
CHANGED
File without changes
|
body
CHANGED
@@ -140,7 +140,7 @@
|
|
140
140
|
end
|
141
141
|
```
|
142
142
|
|
143
|
-
|
143
|
+
|
144
144
|
app/controllers/relationships_controller.rb
|
145
145
|
|
146
146
|
```
|
3
sz
title
CHANGED
File without changes
|
body
CHANGED
@@ -5,10 +5,12 @@
|
|
5
5
|

|
6
6
|
|
7
7
|
##【推測】
|
8
|
-
・user_id
|
8
|
+
・user_idの値が渡せていない。
|
9
9
|
|
10
|
+
・gym_idである2の値は渡せている。
|
10
11
|
|
11
12
|
|
13
|
+
|
12
14
|
記事中では、<%= render 'relationships/follow', user: @user %>を好きなところにおけば良いということでしたが動きませんでした。
|
13
15
|
|
14
16
|
|
2
あ
title
CHANGED
File without changes
|
body
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
現在、下記記事を参考にしてフォロー機能を実装しています。
|
2
|
+
https://qiita.com/mitsumitsu1128/items/e41e2ff37f143db81897#relationships%E3%82%B3%E3%83%B3%E3%83%88%E3%83%AD%E3%83%BC%E3%83%A9%E3%82%92%E4%BD%9C%E6%88%90%E7%B7%A8%E9%9B%86
|
2
3
|
|
3
4
|
下記のエラーが表示されています。
|
4
5
|

|
1
s
title
CHANGED
File without changes
|
body
CHANGED
@@ -11,6 +11,8 @@
|
|
11
11
|
記事中では、<%= render 'relationships/follow', user: @user %>を好きなところにおけば良いということでしたが動きませんでした。
|
12
12
|
|
13
13
|
|
14
|
+
##コード
|
15
|
+
|
14
16
|
フォローボタンを配置したい場所はgymコントローラのshowアクションになります。
|
15
17
|
views/gyms/show.html.erb
|
16
18
|
|