質問編集履歴
2
rails routes の結果を載せました。
title
CHANGED
File without changes
|
body
CHANGED
@@ -4,17 +4,33 @@
|
|
4
4
|
フォームには、`method: :post`と、指定しているのですが、なぜかこのエラーが出てしまいます。
|
5
5
|
|
6
6
|
`remote: true`を消して、Ajaxを無効にしたら、正常に動作することは確認しています。
|
7
|
+
posts/show.html.erb
|
8
|
+
```
|
9
|
+
<li class="activity-list__item" id="like_form">
|
10
|
+
<%= render'likes/like_form' %>
|
11
|
+
</li>
|
12
|
+
```
|
7
13
|
|
8
14
|
likes/_like_form.html.erb
|
9
15
|
```
|
10
16
|
<% if user_signed_in? %>
|
11
|
-
<div id="like_form">
|
12
17
|
<% if current_user.already_liked?(@post) %>
|
18
|
+
<span>
|
19
|
+
<%= link_to(post_like_path(@post), method: :delete, remote: true) do %>
|
13
|
-
|
20
|
+
<%= image_tag 'gooded.svg', class: "good-icon" %>
|
21
|
+
<% end %>
|
22
|
+
<p class="arrow_box">いいねを外す</p>
|
23
|
+
</span>
|
24
|
+
<%= @post.likes.count %>
|
14
25
|
<% else %>
|
26
|
+
<span>
|
27
|
+
<%= link_to(post_likes_path(@post), method: :post, remote: true) do %>
|
15
|
-
|
28
|
+
<%= image_tag 'good.svg', class: "good-icon" %>
|
29
|
+
<% end %>
|
30
|
+
<p class="arrow_box">いいね!</p>
|
31
|
+
</span>
|
32
|
+
<%= @post.likes.count %>
|
16
33
|
<% end %>
|
17
|
-
</div>
|
18
34
|
<% else %>
|
19
35
|
<span>
|
20
36
|
<%= image_tag 'gooded.svg', class: "good-icon" %>
|
@@ -22,24 +38,6 @@
|
|
22
38
|
</span>
|
23
39
|
<% end %>
|
24
40
|
```
|
25
|
-
likes/_like.html.erb
|
26
|
-
```
|
27
|
-
<span>
|
28
|
-
<%= link_to(post_likes_path(@post), method: :post, remote: true) do %>
|
29
|
-
<%= image_tag 'good.svg', class: "good-icon" %>
|
30
|
-
<% end %>
|
31
|
-
<p class="arrow_box">いいね!</p>
|
32
|
-
</span>
|
33
|
-
```
|
34
|
-
likes/_unlike.html.erb
|
35
|
-
```
|
36
|
-
<span>
|
37
|
-
<%= link_to(post_like_path(@post), method: :delete, remote: true) do %>
|
38
|
-
<%= image_tag 'gooded.svg', class: "good-icon" %>
|
39
|
-
<% end %>
|
40
|
-
<p class="arrow_box">いいねを外す</p>
|
41
|
-
</span>
|
42
|
-
```
|
43
41
|
|
44
42
|
likes_controller.rb
|
45
43
|
```
|
@@ -48,20 +46,12 @@
|
|
48
46
|
def create
|
49
47
|
@like = current_user.likes.create(post_id: params[:post_id])
|
50
48
|
@post = Post.find(params[:post_id])
|
51
|
-
respond_to do |format|
|
52
|
-
format.html { redirect_back(fallback_location: root_path) }
|
53
|
-
format.js
|
54
|
-
end
|
55
49
|
end
|
56
50
|
|
57
51
|
def destroy
|
58
52
|
@like = Like.find_by(post_id: params[:post_id], user_id: current_user.id)
|
59
53
|
@post = Post.find(params[:post_id])
|
60
54
|
@like.destroy
|
61
|
-
respond_to do |format|
|
62
|
-
format.html { redirect_back(fallback_location: root_path) }
|
63
|
-
format.js
|
64
|
-
end
|
65
55
|
end
|
66
56
|
|
67
57
|
def index
|
@@ -72,17 +62,19 @@
|
|
72
62
|
@posts << Post.find_by(id: like.post_id)
|
73
63
|
end
|
74
64
|
end
|
75
|
-
|
76
65
|
end
|
66
|
+
|
77
67
|
```
|
78
68
|
lises/create.js.erb
|
79
69
|
```js
|
80
|
-
$(
|
70
|
+
$('#like_form').html("<%= j(render partial: 'likes/like_form') %>");
|
71
|
+
|
81
72
|
```
|
82
73
|
|
83
74
|
likes/destroy.js.erb
|
84
75
|
```js
|
85
|
-
$(
|
76
|
+
$('#like_form').html("<%= j(render partial: 'likes/like_form') %>");
|
77
|
+
|
86
78
|
```
|
87
79
|
|
88
80
|
routes.rb
|
@@ -102,4 +94,52 @@
|
|
102
94
|
resources :comments, only: %i[create edit]
|
103
95
|
end
|
104
96
|
end
|
105
|
-
```
|
97
|
+
```
|
98
|
+
|
99
|
+
`$rails routes `の結果
|
100
|
+
```
|
101
|
+
root GET / home#index
|
102
|
+
new_user_session GET /users/sign_in(.:format) devise/sessions#new
|
103
|
+
user_session POST /users/sign_in(.:format) devise/sessions#create
|
104
|
+
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
|
105
|
+
new_user_password GET /users/password/new(.:format) devise/passwords#new
|
106
|
+
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
|
107
|
+
user_password PATCH /users/password(.:format) devise/passwords#update
|
108
|
+
PUT /users/password(.:format) devise/passwords#update
|
109
|
+
POST /users/password(.:format) devise/passwords#create
|
110
|
+
cancel_user_registration GET /users/cancel(.:format) devise/registrations#cancel
|
111
|
+
new_user_registration GET /users/sign_up(.:format) devise/registrations#new
|
112
|
+
edit_user_registration GET /users/edit(.:format) devise/registrations#edit
|
113
|
+
user_registration PATCH /users(.:format) devise/registrations#update
|
114
|
+
PUT /users(.:format) devise/registrations#update
|
115
|
+
DELETE /users(.:format) devise/registrations#destroy
|
116
|
+
POST /users(.:format) devise/registrations#create
|
117
|
+
user_likes GET /users/:user_id/likes(.:format) likes#index
|
118
|
+
following_user GET /users/:id/following(.:format) users#following
|
119
|
+
followers_user GET /users/:id/followers(.:format) users#followers
|
120
|
+
users GET /users(.:format) users#index
|
121
|
+
user GET /users/:id(.:format) users#show
|
122
|
+
relationships POST /relationships(.:format) relationships#create
|
123
|
+
relationship DELETE /relationships/:id(.:format) relationships#destroy
|
124
|
+
post_likes POST /posts/:post_id/likes(.:format) likes#create
|
125
|
+
post_like DELETE /posts/:post_id/likes/:id(.:format) likes#destroy
|
126
|
+
post_comments POST /posts/:post_id/comments(.:format) comments#create
|
127
|
+
edit_post_comment GET /posts/:post_id/comments/:id/edit(.:format) comments#edit
|
128
|
+
posts GET /posts(.:format) posts#index
|
129
|
+
POST /posts(.:format) posts#create
|
130
|
+
new_post GET /posts/new(.:format) posts#new
|
131
|
+
post GET /posts/:id(.:format) posts#show
|
132
|
+
DELETE /posts/:id(.:format) posts#destroy
|
133
|
+
rails_service_blob GET /rails/active_storage/blobs/:signed_id/*filename(.:format) active_storage/blobs#show
|
134
|
+
rails_blob_representation GET /rails/active_storage/representations/:signed_blob_id/:variation_key/*filename(.:format) active_storage/representations#show
|
135
|
+
rails_disk_service GET /rails/active_storage/disk/:encoded_key/*filename(.:format) active_storage/disk#show
|
136
|
+
update_rails_disk_service PUT /rails/active_storage/disk/:encoded_token(.:format) active_storage/disk#update
|
137
|
+
rails_direct_uploads POST /rails/active_storage/direct_uploads(.:format) active_storage/direct_uploads#create
|
138
|
+
```
|
139
|
+
|
140
|
+
エラーの全文は以下のとうりです。
|
141
|
+
```
|
142
|
+
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]):
|
143
|
+
```
|
144
|
+
|
145
|
+
エラーは Ajaxでの更新時のみに発生して、画面を更新すると、ボタンを押した結果も更新されていて、正常に表示されます。
|
1
route.rbを追加しました!
title
CHANGED
File without changes
|
body
CHANGED
@@ -83,4 +83,23 @@
|
|
83
83
|
likes/destroy.js.erb
|
84
84
|
```js
|
85
85
|
$("#like_form").html("<%= escape_javascript(render('likes/like')) %>");
|
86
|
+
```
|
87
|
+
|
88
|
+
routes.rb
|
89
|
+
```
|
90
|
+
Rails.application.routes.draw do
|
91
|
+
root to: 'home#index'
|
92
|
+
devise_for :users
|
93
|
+
resources :users, only: %i[index show] do
|
94
|
+
resources :likes, only: %i[index]
|
95
|
+
member do
|
96
|
+
get :following, :followers
|
97
|
+
end
|
98
|
+
end
|
99
|
+
resources :relationships, only: %i[create destroy]
|
100
|
+
resources :posts, only: %i[index new show create destroy search] do
|
101
|
+
resources :likes, only:%i[create destroy]
|
102
|
+
resources :comments, only: %i[create edit]
|
103
|
+
end
|
104
|
+
end
|
86
105
|
```
|