vendredi 24 novembre 2017

My "Delete Comment" function from my Ruby on Rails Blog Website doesn't work and I don't know why

My "Delete Comment" function from my Ruby on Rails Blog Website doesn't work and I don't know why.

Here is my error:

Routing Error No route matches [POST] "/posts/17/comments/9" Rails.root: C:/Sites/blog Routes Routes match in priority from top to bottom

"comments_controller.rb" file:

class CommentsController < ApplicationController
def create
    @post = Post.find(params[:post_id])
    @comment = @post.comments.create(params[:comment].permit(:name, :body))

    redirect_to post_path(@post)
end

def destroy
    @post = Post.find(params[:post_id])
    @comment = @post.comments.find(params[:id])
    @comment.destroy

    redirect_to post_path(@post)
end
end

"_comment.html.erb":

<div class="comment clearfix">
<div class="comment_content">
    <p class="comment_name"><strong><%= comment.name %></strong></p>
    <p class="comment_body"><%= comment.body %></p>
    <p class="comment_time"><%= time_ago_in_words(comment.created_at) %> Ago</p>
</div>

<p>
    <%= link_to 'Delete', [comment.post, comment],
                method: :detele,
                class: "button",
                data: {confirm: 'Are you sure?'} 
    %>
</p>
</div>

"_form.html.erb":

<%= form_for([@post, @post.comments.build]) do |f| %>

<%= f.label :name %><br>
<%= f.text_field :name %><br>

<br>
<%= f.label :body %><br>
<%= f.text_area :body %><br>

<br>
<%= f.submit %>
<% end %>

"show.html.erb":

<div id="post_content">
<h1 class="title">
    <%= @post.title %>
</h1>

<p class="date">
    Submitted <%= time_ago_in_words(@post.created_at) %> Ago
    | <%= link_to 'Edit', edit_post_path(@post) %>
    | <%= link_to 'Delete', post_path(@post),
          method: :delete,
          data: { confirm: 'Are you sure?' } %>
</p>

<p class="body">
    <%= @post.body %>
</p>

<div id="comment">
    <h2><%= @post.comments.count %> Comments </h2>
    <%= render @post.comments %>

    <h3>Add a comment: </h3>
    <%= render "comments/form" %>
</div>
</div>

"posts.rb":

class Post < ApplicationRecord
has_many :comments, dependent: :destroy
validates :title, presence: true
validates :body, presence: true
end

"routes.rb":

Rails.application.routes.draw do

resources :posts do 
    resources :comments
end

root "posts#index"
end

I really don't know how to fix this. Please help me. Thank you! <3

Aucun commentaire:

Enregistrer un commentaire