没有路由匹配 {:action=>"show", :controller=>"comments", :id=>nil, :link_id=>"4"},缺少必需的键:[:id ]

No route matches {:action=>"show", :controller=>"comments", :id=>nil, :link_id=>"4"}, missing required keys: [:id]

我有这个错误;

No route matches {:action=>"show", :controller=>"comments", :id=>nil, :link_id=>"4"}, missing required keys: [:id]

错误来自我的 _comment.html.erb 文件;

<%= comment.content %>
<%= link_to 'Delete', link_comment_path(comment.link, comment) , method: :delete, 
                      data: { confirm: "Are you sure ?"} %>

这是我的 comments_controller.rb 文件;

class CommentsController < ApplicationController

    def create
        @link = Link.find(params[:link_id])
        @comment = @link.comments.create(comment_params)
        @comment.link_id = @link.id
        @comment.user_id = current_user.id
        if @comment.save
         redirect_to link_path(@link)
        else
         render 'new'
        end
    end

    def destroy
        @link = Link.find(params[:link_id])
        @comment = Comment.find[params[:id]]
        @comment.destroy

        redirect_to link_path(@link)
    end

    private
    def comment_params
        params.require(:comment).permit(:content)
    end
end

这是我的 routes.rb 文件;

Rails.application.routes.draw do
  devise_for :users
  resources :links do
    resources :comments
  end
  root "links#index"
end

这是我来自 rails routes 命令的路线;

link_comments     GET    /links/:link_id/comments(.:format)             comments#index                                          
                  POST   /links/:link_id/comments(.:format)             comments#index
new_link_comment  GET    /links/:link_id/comments/new(.:format)         comments#new                                                
edit_link_comment GET    /links/:link_id/comments/:id/edit(.:format)    comments#edit                                         
link_comment      GET    /links/:link_id/comments/:id(.:format)         comments#show                                   
                  PATCH  /links/:link_id/comments/:id(.:format)         comments#update                                       
                  PUT    /links/:link_id/comments/:id(.:format)         comments#update                                        
                  DELETE /links/:link_id/comments/:id(.:format)         comments#destroy                                       

我的想法是,我想做一个黑客新闻克隆。每个 link 页面都有一个与 link 关联的评论。现在,我在实现评论的删除功能时遇到了问题。

我假设 link 中的“评论”是一个 copy/paste 错误,而你的意思是 @comment:

<%= comment.content %>
<%= link_to 'Delete', link_comment_path(@comment.link, @comment),
                      method: :delete, data: { confirm: "Are you sure ?"} %>

但是,我很想将您的评论自行销毁。

路线

Rails.application.routes.draw do
  devise_for :users
  resources :links, shallow: true do
    resources :comments
  end
  root "links#index"
end

控制器

def destroy
  @comment = Comment.find[params[:id]]
  @link = Comment.link
  @comment.destroy

  redirect_to link_path(@link)
end

查看(我讨厌ERB,有一些不错的HAML)

= comment.content
= link_to 'Delete', @comment, method: :delete, data: { confirm: "Are you sure ?"}