質問編集履歴
8
情報の追加
title
CHANGED
File without changes
|
body
CHANGED
@@ -4,8 +4,10 @@
|
|
4
4
|
[この記事を参考に](https://qiita.com/hayabusa3703/items/2b916e652a1dc85bb6e3)
|
5
5
|
いいね機能を実装しました
|
6
6
|
|
7
|
+
# [このアプリのgithub](https://github.com/Harasou21/coffee_passport/tree/liking)
|
7
8
|
|
8
9
|
|
10
|
+
|
9
11
|
### 発生している問題・エラーメッセージ
|
10
12
|

|
11
13
|
|
7
タイトルの修正
title
CHANGED
@@ -1,1 +1,1 @@
|
|
1
|
-
いいね
|
1
|
+
いいねの解除ができない
|
body
CHANGED
File without changes
|
6
最新のコードへ変更
title
CHANGED
@@ -1,1 +1,1 @@
|
|
1
|
-
いいね
|
1
|
+
いいねボタンを押してもいいねが変わらない
|
body
CHANGED
@@ -4,38 +4,43 @@
|
|
4
4
|
[この記事を参考に](https://qiita.com/hayabusa3703/items/2b916e652a1dc85bb6e3)
|
5
5
|
いいね機能を実装しました
|
6
6
|
|
7
|
+
|
8
|
+
|
7
9
|
### 発生している問題・エラーメッセージ
|
8
10
|

|
9
|
-
](ba7333a3e628cef93a831ca2131bb6dc.png)
|
10
11
|
|
11
|
-
|
12
|
+
# いいねボタンを押した時、いいねは上手く行くのですが、いいねの解除が上手くいきません
|
12
13
|
|
13
14
|
**rails serverのログ**では
|
14
15
|
|
15
16
|
```
|
17
|
+
Started DELETE "/like/19" for ::1 at 2020-12-23 16:56:49 +0900
|
18
|
+
Processing by LikesController#unlike as JS
|
19
|
+
Parameters: {"drink_id"=>"19"}
|
20
|
+
Drink Load (0.5ms) SELECT `drinks`.* FROM `drinks` WHERE `drinks`.`id` = 19 LIMIT 1
|
21
|
+
↳ app/controllers/likes_controller.rb:21:in `set_variables'
|
22
|
+
User Load (0.4ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 6 LIMIT 1
|
23
|
+
↳ app/helpers/sessions_helper.rb:53:in `current_user'
|
24
|
+
Like Load (0.3ms) SELECT `likes`.* FROM `likes` WHERE `likes`.`user_id` = 6 AND `likes`.`drink_id` = 19 LIMIT 1
|
25
|
+
↳ app/controllers/likes_controller.rb:14:in `unlike'
|
26
|
+
Rendering likes/unlike.js.erb
|
27
|
+
CACHE Like Load (0.0ms) SELECT `likes`.* FROM `likes` WHERE `likes`.`user_id` = 6 AND `likes`.`drink_id` = 19 LIMIT 1
|
16
|
-
|
28
|
+
↳ app/views/likes/_like.html.erb:2
|
29
|
+
(0.3ms) SELECT COUNT(*) FROM `likes` WHERE `likes`.`drink_id` = 19
|
30
|
+
↳ app/views/likes/_like.html.erb:4
|
17
|
-
Rendered likes/_like.html.erb (Duration:
|
31
|
+
Rendered likes/_like.html.erb (Duration: 3.2ms | Allocations: 1654)
|
18
|
-
Rendered likes/
|
32
|
+
Rendered likes/unlike.js.erb (Duration: 3.5ms | Allocations: 1762)
|
19
|
-
Completed
|
33
|
+
Completed 200 OK in 11ms (Views: 4.1ms | ActiveRecord: 1.5ms | Allocations: 4365)
|
20
34
|
|
21
35
|
|
22
|
-
|
23
|
-
ActionView::Template::Error (undefined local variable or method `drink' for #<#<Class:0x00007ff892254698>:0x00007ff8ace00bd0>
|
24
|
-
Did you mean? @drink
|
25
|
-
@drinks):
|
26
|
-
1: <div class="like" id="like-link-<%= drink.id %>">
|
27
|
-
2: <% if current_user.likes.find_by(drink_id: drink.id) %>
|
28
|
-
3: <%= link_to unlike_path(drink.id), method: :delete, remote: true do %>
|
29
|
-
4: <div class = "iine__button">❤️<%= drink.likes.count %></div>
|
30
|
-
|
31
|
-
app/views/likes/_like.html.erb:1
|
32
|
-
app/views/likes/like.js.erb:1
|
33
36
|
```
|
34
37
|
|
35
|
-
|
38
|
+
しっかり、いいねを取り消すunlikeメソッドが反応してます
|
36
39
|
|
37
|
-
|
40
|
+
コンソール上ではエラーは確認されてません
|
38
41
|
|
42
|
+
きっとレンダリングが上手く行ってない可能性が高いと思います
|
43
|
+
|
39
44
|
### 該当のソースコード
|
40
45
|
|
41
46
|
**drinks/index.erb**
|
@@ -80,7 +85,7 @@
|
|
80
85
|
#<%=tag.tag_name%>
|
81
86
|
<%end%>
|
82
87
|
</div>
|
83
|
-
<%= render
|
88
|
+
<%= render partial: "likes/like",locals:{drink: drink}%>
|
84
89
|
</div>
|
85
90
|
|
86
91
|
<% end %>
|
@@ -108,15 +113,17 @@
|
|
108
113
|
**likes/like.js.erb**
|
109
114
|
|
110
115
|
```erb
|
111
|
-
$(<%= @id_name %>).html('<%= escape_javascript(render("likes/like"
|
116
|
+
$("<%= @id_name %>").html('<%= escape_javascript(render("likes/like", drink: @drink )) %>');
|
112
117
|
```
|
113
118
|
|
114
119
|
**likes/unlike.js.erb**
|
115
120
|
|
116
121
|
```erb
|
117
|
-
$(<%= @id_name %>).html('<%= escape_javascript(render("likes/like",
|
122
|
+
$("<%= @id_name %>").html('<%= escape_javascript(render("likes/like", drink: @drink )) %>');
|
118
123
|
```
|
119
124
|
|
125
|
+
こっちが上手くいかない。
|
126
|
+
|
120
127
|
**like_controller**
|
121
128
|
|
122
129
|
```ruby
|
@@ -189,41 +196,11 @@
|
|
189
196
|
|
190
197
|
### 試したこと
|
191
198
|
|
192
|
-
**rails serverのlikes/_like.html.erbで**
|
193
199
|
|
194
|
-
```
|
195
|
-
ActionView::Template::Error (undefined local variable or method `drink' for #<#<Class:0x00007ff66423e8f0>:0x00007ff66423c5a0>
|
196
|
-
Did you mean? @drink
|
197
|
-
@drinks):
|
198
|
-
1: <div class="like" id="like-link-<%= drink.id %>">
|
199
|
-
2: <% if current_user.likes.find_by(drink_id: drink.id) %>
|
200
|
-
3: <%= link_to unlike_path(drink.id), method: :delete, remote: true do %>
|
201
|
-
4: <div class = "iine__button">❤️<%= drink.likes.count %></div>
|
202
|
-
|
203
|
-
app/views/likes/_like.html.erb:1
|
204
|
-
app/views/likes/unlike.js.erb:1
|
205
200
|
|
201
|
+
とあったので、drinkの部分を@drinkに全部
|
206
202
|
|
207
|
-
```
|
208
|
-
|
209
|
-
とあったので、drinkの部分を@drinkに全部変更したら、
|
210
|
-
undefined method `id' for nil:NilClass
|
211
|
-
とエラーが出ました
|
212
|
-
|
213
|
-
パーシャルでローカル変数の受け渡しが上手く行ってない可能性が高いですが、自分はこれ以上わかりませんでした。
|
214
|
-
|
215
|
-
drinkも
|
216
|
-
**drinks/index.html.erb**
|
217
|
-
|
218
|
-
で
|
219
|
-
|
220
203
|
```erb
|
221
|
-
<%= render 'likes/like',locals:{ drink: drink}%>
|
222
|
-
```
|
223
|
-
|
224
|
-
とdrinkをしっかり定義しているので原因がわかりません。
|
225
|
-
|
226
|
-
```erb
|
227
204
|
$(<%= @id_name %>).html('<%= escape_javascript(render("likes/like" ,locals: {drink: @drink})) %>');
|
228
205
|
```
|
229
206
|
|
5
情報の追加
title
CHANGED
File without changes
|
body
CHANGED
@@ -108,13 +108,13 @@
|
|
108
108
|
**likes/like.js.erb**
|
109
109
|
|
110
110
|
```erb
|
111
|
-
$(
|
111
|
+
$(<%= @id_name %>).html('<%= escape_javascript(render("likes/like" ,locals: {drink: @drink})) %>');
|
112
112
|
```
|
113
113
|
|
114
114
|
**likes/unlike.js.erb**
|
115
115
|
|
116
116
|
```erb
|
117
|
-
$(
|
117
|
+
$(<%= @id_name %>).html('<%= escape_javascript(render("likes/like",locals: {drink: @drink} )) %>');
|
118
118
|
```
|
119
119
|
|
120
120
|
**like_controller**
|
@@ -223,6 +223,21 @@
|
|
223
223
|
|
224
224
|
とdrinkをしっかり定義しているので原因がわかりません。
|
225
225
|
|
226
|
+
```erb
|
227
|
+
$(<%= @id_name %>).html('<%= escape_javascript(render("likes/like" ,locals: {drink: @drink})) %>');
|
228
|
+
```
|
229
|
+
|
230
|
+
に元々
|
231
|
+
|
232
|
+
```erb
|
233
|
+
$("<%= @id_name %>").html('<%= escape_javascript(render("likes/like" ,locals: {drink: @drink})) %>');
|
234
|
+
```
|
235
|
+
このように
|
236
|
+
ダブルクォーテーションを入れてたのですが、
|
237
|
+
likes_contorollerで
|
238
|
+
@id_name = "#like-link-#{@drink.id}"
|
239
|
+
と定義してたので、ダブルクォーテーションを外しても結果は変わりませんでした
|
240
|
+
|
226
241
|
また、binding.pry(デバッガーツール)で
|
227
242
|
likes_contorollerのiike,unlikeアクションはしっかり反応して、処理を止めてくれて、変数の中身にしっかり値が入ってくれました。
|
228
243
|
|
4
情報の追加
title
CHANGED
File without changes
|
body
CHANGED
@@ -222,4 +222,8 @@
|
|
222
222
|
```
|
223
223
|
|
224
224
|
とdrinkをしっかり定義しているので原因がわかりません。
|
225
|
+
|
226
|
+
また、binding.pry(デバッガーツール)で
|
227
|
+
likes_contorollerのiike,unlikeアクションはしっかり反応して、処理を止めてくれて、変数の中身にしっかり値が入ってくれました。
|
228
|
+
|
225
229
|
御回答いただければ幸いです。
|
3
情報の追加
title
CHANGED
File without changes
|
body
CHANGED
@@ -192,10 +192,18 @@
|
|
192
192
|
**rails serverのlikes/_like.html.erbで**
|
193
193
|
|
194
194
|
```
|
195
|
-
ActionView::Template::Error (undefined local variable or method `drink' for #<#<Class:
|
195
|
+
ActionView::Template::Error (undefined local variable or method `drink' for #<#<Class:0x00007ff66423e8f0>:0x00007ff66423c5a0>
|
196
|
-
Did you mean? @drink
|
196
|
+
Did you mean? @drink
|
197
|
+
@drinks):
|
197
198
|
1: <div class="like" id="like-link-<%= drink.id %>">
|
198
|
-
2: <% if current_user.likes.find_by(drink_id:
|
199
|
+
2: <% if current_user.likes.find_by(drink_id: drink.id) %>
|
200
|
+
3: <%= link_to unlike_path(drink.id), method: :delete, remote: true do %>
|
201
|
+
4: <div class = "iine__button">❤️<%= drink.likes.count %></div>
|
202
|
+
|
203
|
+
app/views/likes/_like.html.erb:1
|
204
|
+
app/views/likes/unlike.js.erb:1
|
205
|
+
|
206
|
+
|
199
207
|
```
|
200
208
|
|
201
209
|
とあったので、drinkの部分を@drinkに全部変更したら、
|
2
最新のコードへ変更
title
CHANGED
File without changes
|
body
CHANGED
@@ -124,7 +124,7 @@
|
|
124
124
|
include SessionsHelper
|
125
125
|
|
126
126
|
before_action :set_variables
|
127
|
-
def
|
127
|
+
def like
|
128
128
|
|
129
129
|
like = current_user.likes.new(drink_id: @drink.id)
|
130
130
|
#redirect_to drinks_path
|
@@ -133,7 +133,7 @@
|
|
133
133
|
like.save
|
134
134
|
end
|
135
135
|
|
136
|
-
def
|
136
|
+
def unlike
|
137
137
|
like = current_user.likes.find_by(drink_id: @drink.id)
|
138
138
|
binding.pry
|
139
139
|
like.destroy
|
1
最新のコードへ変更
title
CHANGED
File without changes
|
body
CHANGED
@@ -13,16 +13,16 @@
|
|
13
13
|
**rails serverのログ**では
|
14
14
|
|
15
15
|
```
|
16
|
-
↳ app/controllers/likes_controller.rb:23:in `set_variables'
|
17
|
-
|
16
|
+
↳ app/views/likes/like.js.erb:1
|
18
|
-
Rendered likes/_like.html.erb (Duration:
|
17
|
+
Rendered likes/_like.html.erb (Duration: 464.9ms | Allocations: 199855)
|
19
|
-
Rendered likes/like.js.erb (Duration:
|
18
|
+
Rendered likes/like.js.erb (Duration: 465.6ms | Allocations: 200200)
|
20
|
-
Completed 500 Internal Server Error in
|
19
|
+
Completed 500 Internal Server Error in 14458ms (ActiveRecord: 2.9ms | Allocations: 220526)
|
21
20
|
|
22
21
|
|
23
22
|
|
24
|
-
ActionView::Template::Error (undefined local variable or method `drink' for #<#<Class:
|
23
|
+
ActionView::Template::Error (undefined local variable or method `drink' for #<#<Class:0x00007ff892254698>:0x00007ff8ace00bd0>
|
25
|
-
Did you mean? @drink
|
24
|
+
Did you mean? @drink
|
25
|
+
@drinks):
|
26
26
|
1: <div class="like" id="like-link-<%= drink.id %>">
|
27
27
|
2: <% if current_user.likes.find_by(drink_id: drink.id) %>
|
28
28
|
3: <%= link_to unlike_path(drink.id), method: :delete, remote: true do %>
|
@@ -32,7 +32,7 @@
|
|
32
32
|
app/views/likes/like.js.erb:1
|
33
33
|
```
|
34
34
|
|
35
|
-
また
|
35
|
+
またFailed to load resource: the server responded with a status of 500 (Internal Server Error)というエラーもコンソール上で確認できました。
|
36
36
|
|
37
37
|
このようなエラーがおきました
|
38
38
|
|
@@ -126,7 +126,7 @@
|
|
126
126
|
before_action :set_variables
|
127
127
|
def create
|
128
128
|
|
129
|
-
like = current_user.likes.new(drink_id: drink.id)
|
129
|
+
like = current_user.likes.new(drink_id: @drink.id)
|
130
130
|
#redirect_to drinks_path
|
131
131
|
# jsを用いるので画面遷移は行わない
|
132
132
|
binding.pry
|
@@ -134,7 +134,7 @@
|
|
134
134
|
end
|
135
135
|
|
136
136
|
def destroy
|
137
|
-
like = current_user.likes.find_by(drink_id: drink.id)
|
137
|
+
like = current_user.likes.find_by(drink_id: @drink.id)
|
138
138
|
binding.pry
|
139
139
|
like.destroy
|
140
140
|
end
|