我是构建测试博客,尝试删除文章评论,发现path
错误。
show.html.erb
<div class="panel panel-default">
<div class="panel-heading"><%= @article.title %></div>
<div class="panel-body">
<p> created at: <%= @article.created_at.strftime("%Y-%m-%d") %> </p>
<%= @article.body %>
<div class="comments">
<h2><%= @article.comments.count %> comments</h2>
<%= render @article.comments %>
</div>
</div>
<div class="panel-footer">
<%= link_to "back", root_path, class: "btn btn-default" %>
<%= link_to "edit", edit_article_path, class: "btn btn-primary" %>
<%= link_to "destroy", article_path(@article), method: :delete, class: "btn btn-danger", data: { confirm: "Are you sure?" } %>
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading">
add comment
</div>
<div class="panel-body">
<%= render "comments/form" %>
</div>
</div>
_comment.html.erb:
<div class="comment clearfix">
<div class="comment_content">
<p class="comment_name">
<%= comment.name %>
</p>
<p class="comment_body">
<%= comment.body %>
</p>
<p class="comment_time">
<%= time_ago_in_words(comment.created_at) %> Ago
</p>
<%= link_to "destroy", article_comment_path([@article, @comment]), method: :delete, data: { confirm: "Are you sure?" } %>
</div>
</div>
comments_controller.rb
class CommentsController < ApplicationController
before_action :get_article, only: [:create, :destroy]
def create
@comment = @article.comments.create(comment_params)
@comment.user_id = current_user.id
if verify_rucaptcha?(@comment) && @comment.save
redirect_to @article, notice: 'add comment successed.'
else
redirect_to article_path(@article), notice: "recaptcha error!"
end
end
def destroy
@comment = @article.comments.find(params[:comment_id])
if @comment.destroy
redirect_to @article
end
end
private
def get_article
@article = Article.find(params[:article_id])
end
def comment_params
params[:comment].permit(:name, :body)
end
end
的routes.rb
Rails.application.routes.draw do
mount RuCaptcha::Engine => "/rucaptcha"
devise_for :users
resources :articles do
resources :comments
end
root 'articles#index'
end
路线:
article_comments GET /articles/:article_id/comments(.:format) comments#index
POST /articles/:article_id/comments(.:format) comments#create
new_article_comment GET /articles/:article_id/comments/new(.:format) comments#new
edit_article_comment GET /articles/:article_id/comments/:id/edit(.:format) comments#edit
article_comment GET /articles/:article_id/comments/:id(.:format) comments#show
PATCH /articles/:article_id/comments/:id(.:format) comments#update
PUT /articles/:article_id/comments/:id(.:format) comments#update
DELETE /articles/:article_id/comments/:id(.:format) comments#destroy
show:/ articles / 7%2F / comments / 7
<%= link_to "destroy", [@article, comment], method: :delete, data: { confirm: "Are you sure?" } %>
show:href =&#34; / articles / 7 / comments / 3&#34;
destroy routes
哪些地方错了?
答案 0 :(得分:0)
有两个问题:
<%= link_to "destroy", article_comment_path(@article, @comment), method: :delete, data: { confirm: "Are you sure?" } %>
您正在使用'[]'括号。params[:comment_id]
params[:id]
醇>
让我知道您的反馈! 感谢