質問編集履歴
1
Ajax対象を変更しました
title
CHANGED
File without changes
|
body
CHANGED
@@ -1,43 +1,76 @@
|
|
1
|
-
Rails
|
1
|
+
Railsで[railsとjsを使ったお手軽「いいね♡機能」](http://qiita.com/YuitoSato/items/94913d6a349a530b2ea2)をみながら、「いいね♡機能」を実装しているのですが、[いいね!ボタン]を押したあとに**リロードしなければ**画面が変化しません。Ajaxでリロードなしで変更できるようにしたいのですが、Rails側でエラーが出ているのですが、解決できません。。
|
2
2
|
|
3
|
+
> エラーメッセージ
|
3
4
|
```Ruby
|
5
|
+
Rendered likes/_like.html.erb (270.1ms)
|
6
|
+
Rendered likes/create.js.erb (288.5ms)
|
7
|
+
Completed 500 Internal Server Error in 381ms (ActiveRecord: 21.7ms)
|
8
|
+
|
4
|
-
ActionView::Template::Error (undefined method `
|
9
|
+
ActionView::Template::Error (undefined local variable or method `micropost' for #<#<Class:0x007f902c142940>:0x007f90284f6658>):
|
5
|
-
1: <%= form_for(current_user.active_relationships.build, remote: true) do |f| %>
|
6
|
-
2: <div><%= hidden_field_tag :followed_id, @user.id %></div>
|
7
|
-
3: <%= f.submit "Follow", type: "submit", class: "btn" %>
|
8
|
-
|
10
|
+
1: <% if logged_in? %>
|
11
|
+
2: <% if micropost.like_user(current_user.id) %>
|
12
|
+
3: <%= button_to micropost_like_path(micropost_id: micropost.id, id: 1), method: :delete, id: "like-button", remote: true do %>
|
13
|
+
4: <%= image_tag("hot.png", style: "width: 20px;") %>
|
14
|
+
5: <span>
|
9
15
|
```
|
10
|
-
現状ですが、ボタンを押してもなんとも起こらずリロードをすると[Follow]→[Unfollow]、[Unfollow]→[Follow]ができます。
|
11
|
-
おそらくAjaxがうまくいっていないのだと思うのですが、なにが悪いかわからず、すみませんがどうぞよろしくお願いいたします。
|
12
16
|
|
17
|
+
###該当するソースコード
|
18
|
+
|
13
|
-
|
19
|
+
> _like.html.erb
|
14
|
-
```
|
20
|
+
```ruby
|
21
|
+
<% if logged_in? %>
|
15
|
-
<%
|
22
|
+
<% if micropost.like_user(current_user.id) %>
|
16
|
-
|
23
|
+
<%= button_to micropost_like_path(micropost_id: micropost.id, id: 1), method: :delete, id: "like-button", remote: true do %>
|
17
|
-
|
24
|
+
<%= image_tag("hot.png", style: "width: 20px;") %>
|
25
|
+
<span>
|
26
|
+
<%= micropost.likes_count %>
|
27
|
+
</span>
|
28
|
+
<% end %>
|
29
|
+
<% else %>
|
30
|
+
<%= button_to micropost_likes_path(micropost),id: "like-button", remote: true do %>
|
31
|
+
<%= image_tag("not.png", style: "width: 20px;") %>
|
32
|
+
<span>
|
33
|
+
<%= micropost.likes_count %>
|
34
|
+
</span>
|
35
|
+
<% end %>
|
36
|
+
<% end %>
|
37
|
+
<% else %>
|
38
|
+
<%= image_tag("not.png") %>
|
39
|
+
<span>
|
40
|
+
<%= micropost.likes_count %>
|
41
|
+
</span>
|
18
42
|
<% end %>
|
19
43
|
```
|
44
|
+
|
20
|
-
|
45
|
+
> create.js.erb
|
46
|
+
|
21
47
|
```Ruby
|
22
|
-
<% unless current_user?(@user) %>
|
23
|
-
<div id="follow_form">
|
24
|
-
<% if current_user.following?(@user) %>
|
25
|
-
<%= render 'unfollow' %>
|
26
|
-
<% else %>
|
27
|
-
|
48
|
+
$("#like-buttons").html("<%= j(render partial: 'like', locals: { microposts: @microposts, likes: @likes, like: @like}) %>")
|
28
|
-
<% end %>
|
29
|
-
</div>
|
30
|
-
<% end %>
|
31
49
|
```
|
32
50
|
|
33
|
-
|
51
|
+
> destroy.js.erb
|
52
|
+
|
34
53
|
```Ruby
|
35
|
-
$("#
|
54
|
+
$("#like-buttons").html("<%= j(render partial: 'like', locals: { microposts: @microposts, likes: @likes }) %>");
|
36
|
-
$("#followers").html('<%= @user.followers.count %>');
|
37
55
|
```
|
38
56
|
|
39
|
-
|
57
|
+
> likes_controller.rb
|
58
|
+
|
40
59
|
```Ruby
|
41
|
-
|
60
|
+
class LikesController < ApplicationController
|
61
|
+
def create
|
42
|
-
|
62
|
+
@like = Like.create(user_id: current_user.id, micropost_id: params[:micropost_id])
|
63
|
+
@likes = Like.where(micropost_id: params[:micropost_id])
|
64
|
+
@microposts = Micropost.all
|
65
|
+
end
|
66
|
+
|
67
|
+
def destroy
|
68
|
+
like = Like.find_by(user_id: current_user.id, micropost_id: params[:micropost_id])
|
69
|
+
like.destroy
|
70
|
+
@likes = Like.where(micropost_id: params[:micropost_id])
|
71
|
+
@microposts = Micropost.all
|
72
|
+
end
|
73
|
+
end
|
43
|
-
```
|
74
|
+
```
|
75
|
+
|
76
|
+
すみませんがどうぞ宜しくお願い致します。
|