投稿へのコメントを削除するとRouting Errorが発生してしまいます。
エラー内容:
ActionController::UrlGenerationError in Posts#show No route matches {:action=>"destroy", :controller=>"comments", :id=>nil, :post_id=>"1"}, missing required keys: [:id]
上記のようにCommentのidが取得できずにいます。
ちなみに、Viewに記載の<%= comment.body %>にはしっかりとそのコメントbodyが記載されます
Routes
1Rails.application.routes.draw do 2 resources :posts do 3 resources :comments, only: [:create, :destroy] 4 end
view
1<% @post .comments.each do |comment| %> 2<%= comment.body %> 3<%= link_to("削除", post_comment_path(@post, comment), {method: "delete"}) %> 4<% end %>
Controller
1class CommentsController < ApplicationController 2 3 def destroy 4 @post = Post.find(params[:post_id]) 5 @comment = @post.comments.find(params[:id]) 6 @comment.destroy 7 redirect_to post_path(@post) 8 end 9 private 10 def comment_params 11 params.require(:comment).permit(:body) 12 end 13end 14
リンクパス↓
post_comment DELETE /posts/:post_id/comments/:id(.:format) comments#destroy
お力添えをお願いいたします。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。