2017-01-12 9 views
0

を破壊し、pathエラーを検出しました。ブログ記事のコメントは、私は記事のコメントを削除しようと、テストのブログを構築したのlink_to問題

nest routes error

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:routes.rbを

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

ショー:/物品/ 7%2F /コメント/ 7

<%= link_to "destroy", [@article, comment], method: :delete, data: { confirm: "Are you sure?" } %> 

ショー:HREF = "/物品/ 7 /コメント/ 3いくつかは、どこが間違っdestroy routes

+0

表示されるエラーメッセージは何ですか? – dp7

+0

'routes.rb'を投稿できますか? '%2F'はフォワードスラッシュ(/)のURLエンコードされた値で、削除パスのどこかに追加しています。 – Vucko

+0

はActiveRecord :: CommentsController#でRecordNotFoundが –

答えて

0

あり二つの問題:<%= link_to "destroy", article_comment_path(@article, @comment), method: :delete, data: { confirm: "Are you sure?" } %>あなたは '[]' ブラケットを使用していた。

  1. はあなたに破壊するリンクを変更します。あなたの破壊方法で
  2. 、私はあなたのフィードバックを知ってみましょうparams[:id]

params[:comment_id]を変更! ありがとう

+0

Noルートマッチ{:アクション=> "ショー"、:のarticle_id =>、ゼロ:コントローラ=> "コメント"、:ID => "6"}欠落必要なキー:[:のarticle_id]は –

+0

私はURLは見られません。 http:// localhost:3000/articles/6 –

+0

'[]'が削除され、ルートは一致しません。 –

関連する問題