teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

2

routeについて

2018/11/14 13:13

投稿

space-hippy
space-hippy

スコア136

answer CHANGED
@@ -15,4 +15,15 @@
15
15
   resources :posts, only: [:index, :show, :create] do
16
16
  resources :comments, only: [:create, :destroy]
17
17
  end
18
- ブロック記法でresources :commentsを囲っていますが、これは必要ないのでは?と思いました
18
+ ブロック記法でresources :commentsを囲っていますが、これは必要ないのでは?と思いました
19
+
20
+ さらに追記
21
+ 上記の様なrouteがネストした状態だと、パスは
22
+ "/comments/#{@post.id}/destroy"
23
+ でなく、
24
+ "/posts/comments/#{@post.id}/destroy"
25
+ になりませんか?
26
+
27
+ いずれにしても、
28
+ delete "comments/:post_id/destroy" => "comments#destroy"
29
+ は不要かと思います

1

route,link_toに関して

2018/11/14 13:13

投稿

space-hippy
space-hippy

スコア136

answer CHANGED
@@ -1,2 +1,18 @@
1
1
  <%=link_to(c.content, "/comments/#{@post.id}/destroy", {method: "delete"})%>
2
- ここが、@post.idではなく、c.idではないでしょうか?
2
+ ここが、@post.idではなく、c.idではないでしょうか?
3
+
4
+ 追記
5
+ resources :comments, only: [:create, :destroy]
6
+ があるので、
7
+ delete "comments/:post_id/destroy" => "comments#destroy"
8
+ は不要かと思いますがいかがですか?
9
+
10
+ また、link_toは、
11
+ <%= link_to '削除', c, method: :delete %>
12
+ でどうでしょうか?
13
+
14
+ また、
15
+  resources :posts, only: [:index, :show, :create] do
16
+ resources :comments, only: [:create, :destroy]
17
+ end
18
+ ブロック記法でresources :commentsを囲っていますが、これは必要ないのでは?と思いました