質問編集履歴

3

修正

2018/12/17 10:41

投稿

ko-u
ko-u

スコア61

test CHANGED
File without changes
test CHANGED
@@ -14,19 +14,9 @@
14
14
 
15
15
  ・Ajexが反応しない
16
16
 
17
- →ブラウザを更新をすると反映はされるのですが、Ajexで実装した部分はエラーになってしっています
17
+ →ブラウザを更新をすると反映はされるのですが、Ajexで切り替わりせん
18
18
 
19
19
 
20
-
21
- 【エラー内容】
22
-
23
- ```
24
-
25
- ActionView::Template::Error (undefined method `id' for #<Post::ActiveRecord_Relation:0x00007fe9d51d8758>):
26
-
27
- 1: $("#like-<%= @post.id %>").html("<%= j(render partial: 'like', locals: { post: @post, likes: @likes }) %>");
28
-
29
- ```
30
20
 
31
21
 
32
22
 
@@ -44,7 +34,7 @@
44
34
 
45
35
  <% if post.like_user(current_user.id) %>
46
36
 
47
- <%= button_to post_like_path(likes, post_id: post.id), method: :delete, id: "like-button", remote: true do %>
37
+ <%= button_to post_like_path(likes, post_id: post.id), method: :delete, id: post.id, remote: true do %>
48
38
 
49
39
  <%= image_tag("icon_red_heart.png") %>
50
40
 
@@ -58,7 +48,7 @@
58
48
 
59
49
  <% else %>
60
50
 
61
- <%= button_to post_likes_path(post),id: "like-button", remote: true do %>
51
+ <%= button_to post_likes_path(post),id: post.id, remote: true do %>
62
52
 
63
53
  <%= image_tag("icon_heart.png") %>
64
54
 
@@ -90,19 +80,7 @@
90
80
 
91
81
  ```
92
82
 
93
- posts_controller
94
83
 
95
- ```
96
-
97
- def index
98
-
99
- @posts = Post.paginate(page: params[:page])
100
-
101
- @likes = Like.where(post_id: params[:post_id])
102
-
103
- end
104
-
105
- ```
106
84
 
107
85
  likes_controller
108
86
 
@@ -114,7 +92,7 @@
114
92
 
115
93
  @likes = Like.where(post_id: params[:post_id])
116
94
 
117
- @post = Post.all
95
+ @post = Post.find(params[:post_id])
118
96
 
119
97
  end
120
98
 
@@ -128,26 +106,8 @@
128
106
 
129
107
  @likes = Like.where(post_id: params[:post_id])
130
108
 
131
- @post = Post.all
109
+ @post = Post.find(params[:post_id])
132
110
 
133
111
  end
134
112
 
135
113
  ```
136
-
137
-
138
-
139
-
140
-
141
- 【試したこと】
142
-
143
- likes_controllerの@post = Post.find(params[:id])に変更↓
144
-
145
-
146
-
147
- ```
148
-
149
- ActiveRecord::RecordNotFound (Couldn't find Post without an ID):
150
-
151
- ```
152
-
153
- というエラーでした。

2

タイトルの変更

2018/12/17 10:41

投稿

ko-u
ko-u

スコア61

test CHANGED
@@ -1 +1 @@
1
- Ajexが反応しない。
1
+ Ajexが反応しない。 【いいね機能の実装】
test CHANGED
File without changes

1

修正

2018/12/17 04:19

投稿

ko-u
ko-u

スコア61

test CHANGED
File without changes
test CHANGED
@@ -14,7 +14,19 @@
14
14
 
15
15
  ・Ajexが反応しない
16
16
 
17
- ・連続でクリックをすると一人ユーザー複数のいいねついてしま
17
+ →ブラウザ更新をすると反映はされるので、Ajexで実装した部分はエラーになってしまっています
18
+
19
+
20
+
21
+ 【エラー内容】
22
+
23
+ ```
24
+
25
+ ActionView::Template::Error (undefined method `id' for #<Post::ActiveRecord_Relation:0x00007fe9d51d8758>):
26
+
27
+ 1: $("#like-<%= @post.id %>").html("<%= j(render partial: 'like', locals: { post: @post, likes: @likes }) %>");
28
+
29
+ ```
18
30
 
19
31
 
20
32
 
@@ -66,7 +78,7 @@
66
78
 
67
79
  ```
68
80
 
69
- $("#like-buttons").html("<%= j(render partial: 'like', locals: { post: post, like: @like, likes: @likes }) %>");
81
+ $("#like-<%= @post.id %>").html("<%= j(render partial: 'like', locals: { post: @post, like: @like, likes: @likes }) %>");
70
82
 
71
83
  ```
72
84
 
@@ -74,7 +86,7 @@
74
86
 
75
87
  ```
76
88
 
77
- $("#like-buttons").html("<%= j(render partial: 'like', locals: { post: post, likes: @likes }) %>");
89
+ $("#like-<%= @post.id %>").html("<%= j(render partial: 'like', locals: { post: @post, likes: @likes }) %>");
78
90
 
79
91
  ```
80
92
 
@@ -98,9 +110,11 @@
98
110
 
99
111
  def create
100
112
 
101
- @like = Like.create(user_id: current_user.id, post_id: params[:post_id])
113
+ @like = current_user.likes.find_or_create_by(post_id: params[:post_id])
102
114
 
103
115
  @likes = Like.where(post_id: params[:post_id])
116
+
117
+ @post = Post.all
104
118
 
105
119
  end
106
120
 
@@ -108,12 +122,32 @@
108
122
 
109
123
  def destroy
110
124
 
111
- like = Like.find_by(user_id: current_user.id, post_id: params[:post_id])
125
+ like = current_user.likes.find_by(post_id: params[:post_id])
112
126
 
113
127
  like.destroy
114
128
 
115
129
  @likes = Like.where(post_id: params[:post_id])
116
130
 
131
+ @post = Post.all
132
+
117
133
  end
118
134
 
119
135
  ```
136
+
137
+
138
+
139
+
140
+
141
+ 【試したこと】
142
+
143
+ likes_controllerの@post = Post.find(params[:id])に変更↓
144
+
145
+
146
+
147
+ ```
148
+
149
+ ActiveRecord::RecordNotFound (Couldn't find Post without an ID):
150
+
151
+ ```
152
+
153
+ というエラーでした。