質問編集履歴

1

Ajax対象を変更しました

2017/08/20 08:50

投稿

marutama_giant
marutama_giant

スコア16

test CHANGED
File without changes
test CHANGED
@@ -1,60 +1,84 @@
1
- Railsチュートリアルの第12ユーザーをフォローする」をやっていま下記のエラーが出てしまい、困ってい
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
- ActionView::Template::Error (undefined method `id' for nil:NilClass):
9
+ Rendered likes/_like.html.erb (270.1ms)
8
10
 
9
- 1: <%= form_for(current_user.active_relationships.build, remote: true) do |f| %>
11
+ Rendered likes/create.js.erb (288.5ms)
10
12
 
11
- 2: <div><%= hidden_field_tag :followed_id, @user.id %></div>
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
- 4: <% end %>
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
- おそらくAjaxがうまくいっていないのだと思うのでが、なにが悪いかわからず、すみませんがどうぞよろしくお願いいたします。
33
+ ###該当るソースコード
22
34
 
23
35
 
24
36
 
25
- _form.html.erb
37
+ > _like.html.erb
26
38
 
27
- ```Ruby
39
+ ```ruby
28
40
 
29
- <%= form_for(current_user.active_relationships.build, remote: true) do |f| %>
41
+ <% if logged_in? %>
30
42
 
31
- <div><%= hidden_field_tag :followed_id, @user.id %></div>
43
+ <% if micropost.like_user(current_user.id) %>
32
44
 
33
- <%= f.submit "Follow", type: "submit", class: "btn" %>
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
- _follow_form.html.erb
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
- <% else %>
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
- </div>
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
- $("#follow_form").html("<%= escape_javascript(render('users/unfollow')) %>");
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
- $("#follow_form").html("<%= escape_javascript(render('users/follow')) %>");
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
+ すみませんがどうぞ宜しくお願い致します。