I'm building a Rails app that has some Polymorphic relationships, which work, however I'm having trouble implementing the destroy action. The Comment table is the polymorphic table, Subject has_many :comments
and profile has_many :comments
. I'm struggling with the ability to Delete comments from the subject show page at the moment(basically a forum). I created an edit_comment_path and can successfully edit and update comments, through the comments controller, however when I try to delete, @comment = Comment.find(params[:id])
it keeps looking for the Subject.id instead of the Comment.id? The link to delete seems correct so i'm a little puzzled. Please see the code below. Any suggestions or refinements would also be greatly appreciated!
<% commentable.comments.each do |comment| %>
<div>
<hr>
<% if commentable == @subject %>
<p><%= comment.body %> | Posted by <%= comment.user.email %>, <%= time_ago_in_words(comment.created_at) + ' ago' %></p>
<% if comment.user == current_user %>
<%= link_to "Edit", edit_comment_path(comment, subject: 'subject') %>
<%= link_to "Delete", comment_path, method: :delete %>
<% end %>
<% else %>
<p><<%= comment.title %></p>
<p><%= comment.body %> </p>
<% end %>
</div>
<% end %>
class CommentsController < ApplicationController
def show
@comment = Comment.find(params[:id])
end
def create
@comment = @commentable.comments.new(comment_params)
@comment.user_id = current_user.id
if @comment.save
redirect_to @commentable, notice: "Successfully Posted!"
end
end
def edit
@comment = Comment.find(params[:id])
end
def update
@comment = Comment.find(params[:id])
if @comment.update(comment_params)
redirect_to @comment.commentable, notice: "Comment was updated."
end
end
def destroy
@comment = Comment.find(params[:id])
end
private
def comment_params
params.require(:comment).permit(:title, :body)
end
end
<h1><%= @subject.title %></h1>
<p>Created by: <%= @subject.user.email %>, <%= time_ago_in_words(@subject.created_at) + ' ago' %></p>
<%= render partial: 'comments/comment', locals: {commentable: @subject} %>
<%= render partial: 'comments/form', locals: {commentable: @subject} %>
Rails.application.routes.draw do
resources :locations, only: [:show, :destroy, :edit, :update]
resources :comments, only: [:show, :edit, :update, :destroy]
resources :subjects, only: [:index, :show] do
resources :comments, module: :subjects
end
resources :profiles do
resources :subjects, module: :profiles
resources :locations, module: :profiles
resources :comments, module: :profiles
end
resource :session, only: [:new, :create, :destroy]
resources :users, only: [:new, :create, :show]
root "home#index"
end
Any feedback would be greatly appreciated
From a quick glance it looks like
<%= link_to "Delete", comment_path, method: :delete %>
should be:
<%= link_to "Delete", comment_path(comment), method: :delete %>
I'm assuming that the problem is that the params[:id]
being passed to your comments#destroy is the Subject's id rather than the Comment's id
Also, your destroy method should look something like:
def destroy
comment = Comment.find(params[:id])
comment.destroy
redirect_to somewhere
end