簡體   English   中英

無法在Rails多態注釋,ActiveRecord中實現銷毀

[英]Trouble implementing Destroy in Rails Polymorphic Comments, ActiveRecord

我正在構建一個具有某些多態關系的Rails應用程序,該關系可以正常工作,但是我在實現destroy動作時遇到了麻煩。 Comment表是多態表,Subject has_many :comments和配置文件has_many :comments 我目前無法從主題顯示頁面(主要是論壇)中刪除評論。 我創建了一個edit_comment_path並可以通過comments控制器成功編輯和更新評論,但是當我嘗試刪除@comment = Comment.find(params[:id])它將一直在尋找Subject.id而不是Comment。 ID? 刪除的鏈接似乎正確,所以我有點困惑。 請參見下面的代碼。 任何建議或改進也將不勝感激!

views / comments / _comment.html.erb

<% 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 %>

comments_controller.rb

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

views / subjects / show.html.erb

<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} %>

config / routes.rb

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

任何反饋將不勝感激

乍一看,看起來像

<%= link_to "Delete", comment_path, method: :delete %>

應該:

<%= link_to "Delete", comment_path(comment), method: :delete %>

我假設問題是傳遞給您的comment#destroy的params[:id]是主題的ID,而不是評論的ID

另外,您的destroy方法應類似於:

def destroy
    comment = Comment.find(params[:id])
    comment.destroy
    redirect_to somewhere
end

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM