質問編集履歴
1
Ajax対象を変更しました
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,60 +1,84 @@
|
|
1
|
-
Rails
|
1
|
+
Railsで[railsとjsを使ったお手軽「いいね♡機能」](http://qiita.com/YuitoSato/items/94913d6a349a530b2ea2)をみながら、「いいね♡機能」を実装しているのですが、[いいね!ボタン]を押したあとに**リロードしなければ**画面が変化しません。Ajaxでリロードなしで変更できるようにしたいのですが、Rails側でエラーが出ているのですが、解決できません。。
|
2
2
|
|
3
3
|
|
4
4
|
|
5
|
+
> エラーメッセージ
|
6
|
+
|
5
7
|
```Ruby
|
6
8
|
|
7
|
-
|
9
|
+
Rendered likes/_like.html.erb (270.1ms)
|
8
10
|
|
9
|
-
|
11
|
+
Rendered likes/create.js.erb (288.5ms)
|
10
12
|
|
11
|
-
|
13
|
+
Completed 500 Internal Server Error in 381ms (ActiveRecord: 21.7ms)
|
12
14
|
|
13
|
-
3: <%= f.submit "Follow", type: "submit", class: "btn" %>
|
14
15
|
|
16
|
+
|
17
|
+
ActionView::Template::Error (undefined local variable or method `micropost' for #<#<Class:0x007f902c142940>:0x007f90284f6658>):
|
18
|
+
|
15
|
-
|
19
|
+
1: <% if logged_in? %>
|
20
|
+
|
21
|
+
2: <% if micropost.like_user(current_user.id) %>
|
22
|
+
|
23
|
+
3: <%= button_to micropost_like_path(micropost_id: micropost.id, id: 1), method: :delete, id: "like-button", remote: true do %>
|
24
|
+
|
25
|
+
4: <%= image_tag("hot.png", style: "width: 20px;") %>
|
26
|
+
|
27
|
+
5: <span>
|
16
28
|
|
17
29
|
```
|
18
30
|
|
19
|
-
現状ですが、ボタンを押してもなんとも起こらずリロードをすると[Follow]→[Unfollow]、[Unfollow]→[Follow]ができます。
|
20
31
|
|
32
|
+
|
21
|
-
|
33
|
+
###該当するソースコード
|
22
34
|
|
23
35
|
|
24
36
|
|
25
|
-
_
|
37
|
+
> _like.html.erb
|
26
38
|
|
27
|
-
```
|
39
|
+
```ruby
|
28
40
|
|
29
|
-
<%
|
41
|
+
<% if logged_in? %>
|
30
42
|
|
31
|
-
<
|
43
|
+
<% if micropost.like_user(current_user.id) %>
|
32
44
|
|
33
|
-
<%=
|
45
|
+
<%= button_to micropost_like_path(micropost_id: micropost.id, id: 1), method: :delete, id: "like-button", remote: true do %>
|
34
46
|
|
35
|
-
<% end %>
|
47
|
+
<%= image_tag("hot.png", style: "width: 20px;") %>
|
36
48
|
|
37
|
-
|
49
|
+
<span>
|
38
50
|
|
39
|
-
|
51
|
+
<%= micropost.likes_count %>
|
40
52
|
|
41
|
-
```Ruby
|
42
|
-
|
43
|
-
<% unless current_user?(@user) %>
|
44
|
-
|
45
|
-
<div id="follow_form">
|
46
|
-
|
47
|
-
<% if current_user.following?(@user) %>
|
48
|
-
|
49
|
-
<%= render 'unfollow' %>
|
50
|
-
|
51
|
-
<
|
53
|
+
</span>
|
52
|
-
|
53
|
-
<%= render 'follow' %>
|
54
54
|
|
55
55
|
<% end %>
|
56
56
|
|
57
|
+
<% else %>
|
58
|
+
|
59
|
+
<%= button_to micropost_likes_path(micropost),id: "like-button", remote: true do %>
|
60
|
+
|
61
|
+
<%= image_tag("not.png", style: "width: 20px;") %>
|
62
|
+
|
63
|
+
<span>
|
64
|
+
|
65
|
+
<%= micropost.likes_count %>
|
66
|
+
|
57
|
-
</
|
67
|
+
</span>
|
68
|
+
|
69
|
+
<% end %>
|
70
|
+
|
71
|
+
<% end %>
|
72
|
+
|
73
|
+
<% else %>
|
74
|
+
|
75
|
+
<%= image_tag("not.png") %>
|
76
|
+
|
77
|
+
<span>
|
78
|
+
|
79
|
+
<%= micropost.likes_count %>
|
80
|
+
|
81
|
+
</span>
|
58
82
|
|
59
83
|
<% end %>
|
60
84
|
|
@@ -62,24 +86,66 @@
|
|
62
86
|
|
63
87
|
|
64
88
|
|
65
|
-
create.js.erb
|
89
|
+
> create.js.erb
|
90
|
+
|
91
|
+
|
66
92
|
|
67
93
|
```Ruby
|
68
94
|
|
69
|
-
$("#
|
95
|
+
$("#like-buttons").html("<%= j(render partial: 'like', locals: { microposts: @microposts, likes: @likes, like: @like}) %>")
|
70
|
-
|
71
|
-
$("#followers").html('<%= @user.followers.count %>');
|
72
96
|
|
73
97
|
```
|
74
98
|
|
75
99
|
|
76
100
|
|
77
|
-
destroy.js.erb
|
101
|
+
> destroy.js.erb
|
102
|
+
|
103
|
+
|
78
104
|
|
79
105
|
```Ruby
|
80
106
|
|
81
|
-
$("#
|
107
|
+
$("#like-buttons").html("<%= j(render partial: 'like', locals: { microposts: @microposts, likes: @likes }) %>");
|
82
|
-
|
83
|
-
$("#followers").html('<%= @user.followers.count %>');
|
84
108
|
|
85
109
|
```
|
110
|
+
|
111
|
+
|
112
|
+
|
113
|
+
> likes_controller.rb
|
114
|
+
|
115
|
+
|
116
|
+
|
117
|
+
```Ruby
|
118
|
+
|
119
|
+
class LikesController < ApplicationController
|
120
|
+
|
121
|
+
def create
|
122
|
+
|
123
|
+
@like = Like.create(user_id: current_user.id, micropost_id: params[:micropost_id])
|
124
|
+
|
125
|
+
@likes = Like.where(micropost_id: params[:micropost_id])
|
126
|
+
|
127
|
+
@microposts = Micropost.all
|
128
|
+
|
129
|
+
end
|
130
|
+
|
131
|
+
|
132
|
+
|
133
|
+
def destroy
|
134
|
+
|
135
|
+
like = Like.find_by(user_id: current_user.id, micropost_id: params[:micropost_id])
|
136
|
+
|
137
|
+
like.destroy
|
138
|
+
|
139
|
+
@likes = Like.where(micropost_id: params[:micropost_id])
|
140
|
+
|
141
|
+
@microposts = Micropost.all
|
142
|
+
|
143
|
+
end
|
144
|
+
|
145
|
+
end
|
146
|
+
|
147
|
+
```
|
148
|
+
|
149
|
+
|
150
|
+
|
151
|
+
すみませんがどうぞ宜しくお願い致します。
|