質問編集履歴
2
rails routes の結果を載せました。
test
CHANGED
File without changes
|
test
CHANGED
@@ -10,6 +10,18 @@
|
|
10
10
|
|
11
11
|
`remote: true`を消して、Ajaxを無効にしたら、正常に動作することは確認しています。
|
12
12
|
|
13
|
+
posts/show.html.erb
|
14
|
+
|
15
|
+
```
|
16
|
+
|
17
|
+
<li class="activity-list__item" id="like_form">
|
18
|
+
|
19
|
+
<%= render'likes/like_form' %>
|
20
|
+
|
21
|
+
</li>
|
22
|
+
|
23
|
+
```
|
24
|
+
|
13
25
|
|
14
26
|
|
15
27
|
likes/_like_form.html.erb
|
@@ -18,20 +30,40 @@
|
|
18
30
|
|
19
31
|
<% if user_signed_in? %>
|
20
32
|
|
21
|
-
<div id="like_form">
|
22
|
-
|
23
33
|
<% if current_user.already_liked?(@post) %>
|
24
34
|
|
35
|
+
<span>
|
36
|
+
|
37
|
+
<%= link_to(post_like_path(@post), method: :delete, remote: true) do %>
|
38
|
+
|
25
|
-
<%=
|
39
|
+
<%= image_tag 'gooded.svg', class: "good-icon" %>
|
40
|
+
|
41
|
+
<% end %>
|
42
|
+
|
43
|
+
<p class="arrow_box">いいねを外す</p>
|
44
|
+
|
45
|
+
</span>
|
46
|
+
|
47
|
+
<%= @post.likes.count %>
|
26
48
|
|
27
49
|
<% else %>
|
28
50
|
|
51
|
+
<span>
|
52
|
+
|
53
|
+
<%= link_to(post_likes_path(@post), method: :post, remote: true) do %>
|
54
|
+
|
29
|
-
<%=
|
55
|
+
<%= image_tag 'good.svg', class: "good-icon" %>
|
56
|
+
|
57
|
+
<% end %>
|
58
|
+
|
59
|
+
<p class="arrow_box">いいね!</p>
|
60
|
+
|
61
|
+
</span>
|
62
|
+
|
63
|
+
<%= @post.likes.count %>
|
30
64
|
|
31
65
|
<% end %>
|
32
66
|
|
33
|
-
</div>
|
34
|
-
|
35
67
|
<% else %>
|
36
68
|
|
37
69
|
<span>
|
@@ -46,42 +78,6 @@
|
|
46
78
|
|
47
79
|
```
|
48
80
|
|
49
|
-
likes/_like.html.erb
|
50
|
-
|
51
|
-
```
|
52
|
-
|
53
|
-
<span>
|
54
|
-
|
55
|
-
<%= link_to(post_likes_path(@post), method: :post, remote: true) do %>
|
56
|
-
|
57
|
-
<%= image_tag 'good.svg', class: "good-icon" %>
|
58
|
-
|
59
|
-
<% end %>
|
60
|
-
|
61
|
-
<p class="arrow_box">いいね!</p>
|
62
|
-
|
63
|
-
</span>
|
64
|
-
|
65
|
-
```
|
66
|
-
|
67
|
-
likes/_unlike.html.erb
|
68
|
-
|
69
|
-
```
|
70
|
-
|
71
|
-
<span>
|
72
|
-
|
73
|
-
<%= link_to(post_like_path(@post), method: :delete, remote: true) do %>
|
74
|
-
|
75
|
-
<%= image_tag 'gooded.svg', class: "good-icon" %>
|
76
|
-
|
77
|
-
<% end %>
|
78
|
-
|
79
|
-
<p class="arrow_box">いいねを外す</p>
|
80
|
-
|
81
|
-
</span>
|
82
|
-
|
83
|
-
```
|
84
|
-
|
85
81
|
|
86
82
|
|
87
83
|
likes_controller.rb
|
@@ -98,65 +94,51 @@
|
|
98
94
|
|
99
95
|
@post = Post.find(params[:post_id])
|
100
96
|
|
97
|
+
end
|
98
|
+
|
99
|
+
|
100
|
+
|
101
|
+
def destroy
|
102
|
+
|
103
|
+
@like = Like.find_by(post_id: params[:post_id], user_id: current_user.id)
|
104
|
+
|
105
|
+
@post = Post.find(params[:post_id])
|
106
|
+
|
107
|
+
@like.destroy
|
108
|
+
|
109
|
+
end
|
110
|
+
|
111
|
+
|
112
|
+
|
113
|
+
def index
|
114
|
+
|
115
|
+
@user = User.find(params[:user_id])
|
116
|
+
|
117
|
+
likes = Like.where(user_id: @user)
|
118
|
+
|
119
|
+
@posts = []
|
120
|
+
|
101
|
-
|
121
|
+
likes.each do |like|
|
102
|
-
|
122
|
+
|
103
|
-
|
123
|
+
@posts << Post.find_by(id: like.post_id)
|
104
|
-
|
105
|
-
format.js
|
106
124
|
|
107
125
|
end
|
108
126
|
|
109
127
|
end
|
110
128
|
|
111
|
-
|
112
|
-
|
113
|
-
def destroy
|
114
|
-
|
115
|
-
@like = Like.find_by(post_id: params[:post_id], user_id: current_user.id)
|
116
|
-
|
117
|
-
@post = Post.find(params[:post_id])
|
118
|
-
|
119
|
-
@like.destroy
|
120
|
-
|
121
|
-
respond_to do |format|
|
122
|
-
|
123
|
-
format.html { redirect_back(fallback_location: root_path) }
|
124
|
-
|
125
|
-
format.js
|
126
|
-
|
127
|
-
end
|
128
|
-
|
129
|
-
end
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
def index
|
134
|
-
|
135
|
-
@user = User.find(params[:user_id])
|
136
|
-
|
137
|
-
likes = Like.where(user_id: @user)
|
138
|
-
|
139
|
-
@posts = []
|
140
|
-
|
141
|
-
likes.each do |like|
|
142
|
-
|
143
|
-
@posts << Post.find_by(id: like.post_id)
|
144
|
-
|
145
|
-
end
|
146
|
-
|
147
|
-
end
|
148
|
-
|
149
|
-
|
150
|
-
|
151
129
|
end
|
152
130
|
|
131
|
+
|
132
|
+
|
153
133
|
```
|
154
134
|
|
155
135
|
lises/create.js.erb
|
156
136
|
|
157
137
|
```js
|
158
138
|
|
159
|
-
$(
|
139
|
+
$('#like_form').html("<%= j(render partial: 'likes/like_form') %>");
|
140
|
+
|
141
|
+
|
160
142
|
|
161
143
|
```
|
162
144
|
|
@@ -166,7 +148,9 @@
|
|
166
148
|
|
167
149
|
```js
|
168
150
|
|
169
|
-
$(
|
151
|
+
$('#like_form').html("<%= j(render partial: 'likes/like_form') %>");
|
152
|
+
|
153
|
+
|
170
154
|
|
171
155
|
```
|
172
156
|
|
@@ -207,3 +191,99 @@
|
|
207
191
|
end
|
208
192
|
|
209
193
|
```
|
194
|
+
|
195
|
+
|
196
|
+
|
197
|
+
`$rails routes `の結果
|
198
|
+
|
199
|
+
```
|
200
|
+
|
201
|
+
root GET / home#index
|
202
|
+
|
203
|
+
new_user_session GET /users/sign_in(.:format) devise/sessions#new
|
204
|
+
|
205
|
+
user_session POST /users/sign_in(.:format) devise/sessions#create
|
206
|
+
|
207
|
+
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
|
208
|
+
|
209
|
+
new_user_password GET /users/password/new(.:format) devise/passwords#new
|
210
|
+
|
211
|
+
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
|
212
|
+
|
213
|
+
user_password PATCH /users/password(.:format) devise/passwords#update
|
214
|
+
|
215
|
+
PUT /users/password(.:format) devise/passwords#update
|
216
|
+
|
217
|
+
POST /users/password(.:format) devise/passwords#create
|
218
|
+
|
219
|
+
cancel_user_registration GET /users/cancel(.:format) devise/registrations#cancel
|
220
|
+
|
221
|
+
new_user_registration GET /users/sign_up(.:format) devise/registrations#new
|
222
|
+
|
223
|
+
edit_user_registration GET /users/edit(.:format) devise/registrations#edit
|
224
|
+
|
225
|
+
user_registration PATCH /users(.:format) devise/registrations#update
|
226
|
+
|
227
|
+
PUT /users(.:format) devise/registrations#update
|
228
|
+
|
229
|
+
DELETE /users(.:format) devise/registrations#destroy
|
230
|
+
|
231
|
+
POST /users(.:format) devise/registrations#create
|
232
|
+
|
233
|
+
user_likes GET /users/:user_id/likes(.:format) likes#index
|
234
|
+
|
235
|
+
following_user GET /users/:id/following(.:format) users#following
|
236
|
+
|
237
|
+
followers_user GET /users/:id/followers(.:format) users#followers
|
238
|
+
|
239
|
+
users GET /users(.:format) users#index
|
240
|
+
|
241
|
+
user GET /users/:id(.:format) users#show
|
242
|
+
|
243
|
+
relationships POST /relationships(.:format) relationships#create
|
244
|
+
|
245
|
+
relationship DELETE /relationships/:id(.:format) relationships#destroy
|
246
|
+
|
247
|
+
post_likes POST /posts/:post_id/likes(.:format) likes#create
|
248
|
+
|
249
|
+
post_like DELETE /posts/:post_id/likes/:id(.:format) likes#destroy
|
250
|
+
|
251
|
+
post_comments POST /posts/:post_id/comments(.:format) comments#create
|
252
|
+
|
253
|
+
edit_post_comment GET /posts/:post_id/comments/:id/edit(.:format) comments#edit
|
254
|
+
|
255
|
+
posts GET /posts(.:format) posts#index
|
256
|
+
|
257
|
+
POST /posts(.:format) posts#create
|
258
|
+
|
259
|
+
new_post GET /posts/new(.:format) posts#new
|
260
|
+
|
261
|
+
post GET /posts/:id(.:format) posts#show
|
262
|
+
|
263
|
+
DELETE /posts/:id(.:format) posts#destroy
|
264
|
+
|
265
|
+
rails_service_blob GET /rails/active_storage/blobs/:signed_id/*filename(.:format) active_storage/blobs#show
|
266
|
+
|
267
|
+
rails_blob_representation GET /rails/active_storage/representations/:signed_blob_id/:variation_key/*filename(.:format) active_storage/representations#show
|
268
|
+
|
269
|
+
rails_disk_service GET /rails/active_storage/disk/:encoded_key/*filename(.:format) active_storage/disk#show
|
270
|
+
|
271
|
+
update_rails_disk_service PUT /rails/active_storage/disk/:encoded_token(.:format) active_storage/disk#update
|
272
|
+
|
273
|
+
rails_direct_uploads POST /rails/active_storage/direct_uploads(.:format) active_storage/direct_uploads#create
|
274
|
+
|
275
|
+
```
|
276
|
+
|
277
|
+
|
278
|
+
|
279
|
+
エラーの全文は以下のとうりです。
|
280
|
+
|
281
|
+
```
|
282
|
+
|
283
|
+
ActionView::Template::Error (No route matches {:action=>"destroy", :controller=>"likes", :post_id=>#<Post id: 23, user_id: 1, created_at: "2019-10-01 01:51:32", updated_at: "2019-10-01 01:51:32", image: nil, picture: nil, title: "タイトル", pictures: ["good.png"], tag_list: nil>}, missing required keys: [:id]):
|
284
|
+
|
285
|
+
```
|
286
|
+
|
287
|
+
|
288
|
+
|
289
|
+
エラーは Ajaxでの更新時のみに発生して、画面を更新すると、ボタンを押した結果も更新されていて、正常に表示されます。
|
1
route.rbを追加しました!
test
CHANGED
File without changes
|
test
CHANGED
@@ -169,3 +169,41 @@
|
|
169
169
|
$("#like_form").html("<%= escape_javascript(render('likes/like')) %>");
|
170
170
|
|
171
171
|
```
|
172
|
+
|
173
|
+
|
174
|
+
|
175
|
+
routes.rb
|
176
|
+
|
177
|
+
```
|
178
|
+
|
179
|
+
Rails.application.routes.draw do
|
180
|
+
|
181
|
+
root to: 'home#index'
|
182
|
+
|
183
|
+
devise_for :users
|
184
|
+
|
185
|
+
resources :users, only: %i[index show] do
|
186
|
+
|
187
|
+
resources :likes, only: %i[index]
|
188
|
+
|
189
|
+
member do
|
190
|
+
|
191
|
+
get :following, :followers
|
192
|
+
|
193
|
+
end
|
194
|
+
|
195
|
+
end
|
196
|
+
|
197
|
+
resources :relationships, only: %i[create destroy]
|
198
|
+
|
199
|
+
resources :posts, only: %i[index new show create destroy search] do
|
200
|
+
|
201
|
+
resources :likes, only:%i[create destroy]
|
202
|
+
|
203
|
+
resources :comments, only: %i[create edit]
|
204
|
+
|
205
|
+
end
|
206
|
+
|
207
|
+
end
|
208
|
+
|
209
|
+
```
|