コメント削除機能実装中なのですがルーティングでエラーが出てしまいました。
解決方法が分からないので教えて頂きたいです。
routes.rb
1Rails.application.routes.draw do 2 3 devise_for :users 4 get "login" => "users#login_form" 5 6 7 post "login" => "users#login" 8 post "logout" => "users#logout" 9 10 11 post "users/:id/update" => "users#update" 12 get "users/:id/edit" => "users#edit" 13 post "users/create" => "users#create" 14 get "singnup" => "users#new" 15 get "users/index" => "users#index" 16 get "users/:id" => "users#show", as: :user 17 18 get "posts/index" => "posts#index" 19 get "posts/new" => "posts#new" 20 get "posts/:id" => "posts#show", as: :post 21 22 resources :comments, only: [:create,:destroy] 23 24 25 post "posts/create" => "posts#create" 26 27 get "posts/:id/edit" => "posts#edit" 28 29 post "posts/:id/update" => "posts#update" 30 post "posts/:id/destroy" => "posts#destroy" 31 get "/" => "home#top" 32 get "about" => "home#about" 33 34end
posts/show.html.erb
1<div class="main posts-show"> 2 <div class="container"> 3 <div class="posts-show-item"> 4 <div class="post-user-name"> 5 <img src="<%= "/user_images/#{@user.image_name}" %>"> 6 7 <%= link_to(@user.name, "/users/#{@user.id}") %> 8 9 </div> 10 11 <p> 12 <%= @post.content %> 13 </p> 14 15 <div class="post-time"> 16 <%= @post.created_at %> 17 </div> 18 <% if @post.user_id == @current_user.id %> 19 <div class="post-menus"> 20 <%= link_to("編集", "/posts/#{@post.id}/edit") %> 21 <%= link_to("削除", "/posts/#{@post.id}/destroy", {method: "post"}) %> 22 </div> 23 <% end %> 24 25<div class="comment-wrapper border-top mb-10"> 26 <p class="mt-5">コメント一覧</p> 27 <% @comments.each do |c| %> 28 <div> 29 <% unless c.user.blank? %> 30 31 <img src="<%= "/user_images/#{c.user.image_name}" %>"> 32 <% end %> 33 <%= c.user.name unless c.user.blank? %> 34 <br /> 35 <%= c.content %> 36 <% @comments.each do |comment| %> 37 38 <%=link_to "削除", comment_path(comment.post.id), method: :delete %> 39<% end %> 40 </div> 41 <br /> 42 <% end %> 43 <% if user_signed_in? %> 44 <%= form_with(model: @comment, url: comments_path, method: :post, local: true) do |f| %> 45 <%= hidden_field_tag :post_id, @post.id %> 46 <%= f.text_area :content, class: "form-control", rows: 5 %> 47 <%= button_tag type: "submit", class: "btn btn-success float-right mt-1" do %> 48 <i class="far fa-comments"></i> コメントする 49 <% end %> 50 <% end %> 51 <% end %> 52</div> 53 </div> 54 55 </div> 56 57</div>
models/comment.rb
1class Comment < ApplicationRecord 2 belongs_to :user 3 belongs_to :post 4end
comments.controller.rb
1class CommentsController < ApplicationController 2 3 before_action :authenticate_user 4 5 def create 6 logger.debug "*"*100 7 logger.debug "[comments_controller.rb]" 8 logger.debug "*"*100 9 post = Post.find(params[:post_id]) 10 @comment = post.comments.build(comment_params) 11 @comment.user_id = current_user.id 12 if @comment.save 13 logger.debug "saved" 14 flash[:notice] = "コメントしました" 15 redirect_to(post) 16 else 17 logger.debug "failed" 18 flash[:notice] = "コメントできませんでした" 19 redirect_to(post) 20 end 21 end 22 23 def destroy 24 Comment.find_by(id: params[:id]) 25 flash[:notice] = "コメントを削除しました" 26 redirect_to("/post") 27 end 28 29 private 30 31 def comment_params 32 params.require(:comment).permit(:content) 33 end 34 logger.debug "*"*100 35end
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。