dimanche 13 mars 2016

NameError for destroy (delete button not working)

I have a comments page within my Ruby file to which I am including a "Delete" button using the destroy method. When I run this on my local host, it is throwing an exception as follows;

NameError in Admin::CommentsController#destroy
uninitialized constant Comment::Notificaiton

Extracted source (around line #11):

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

    redirect_to :back, notice: 'Successfully deleted comment'
  end

Please note: Line 11 is in reference to the @comment.destroy line!

This is my full comments_controller view;

class Admin::CommentsController < Admin::ApplicationController
  def index
    @comments = Comment.where(status: to_bool(params[:status]))
  end

  def update
  end

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

    redirect_to :back, notice: 'Successfully deleted comment'
  end
end

And this is my routes view;

Rails.application.routes.draw do

get '/login' => 'admin/sessions#new'
get '/logout' => 'admin/sessions#destroy'

namespace :admin do
    resources :posts
    resources :comments, only: [:index, :update, :destroy]
    resources :tags, except: [:index]
    resources :sessions, only: [:new, :create, :destroy]
    resources :administrators, only: [:index, :edit, :update]
end
end

And this is my index for the comments view;

<h1>Comments</h1>

<p>
    <%= link_to 'Approved', admin_comments_path(status: true) %>
    <%= link_to 'Un-Approved', admin_comments_path(status: false) %>
</p>

<% @comments.each do |comment| %>
<p><b><%= comment.user.fullname %></b> posted message on <b><%= comment.post.title %></b></p>
<p><%= comment.message %></p>
<p><%= link_to 'Delete', admin_comment_path(comment), method: :delete, data: {confirm: 'Are you sure?'} %></p>
<hr>
<% end %>

Please could you explain why I am having this issue?

Thank you

Aucun commentaire:

Enregistrer un commentaire