joeyk16
joeyk16

Reputation: 1415

Simple form gem - Can't destroy comment

Im making a Reddit Clone and have just added comments.

Everything seems to work except i can't destroy a comment.

When i click Destroy it takes me to an page where i can view the comment. Yo can see the page here http://postimg.org/delete/ehlqrvngq/.

Here is my server when i click Destroy http://postimg.org/delete/equ2hefk8/

Here is my _comments.html.erb

<%= div_for(comment) do %>
    <div class="comments_wrapper clearfix">
        <div class="pull-left">
            <p class="lead"><%= comment.body %></p>
            <p><small>Submitted <strong><%= time_ago_in_words(comment.created_at) %> ago</strong> by <%= comment.user.name %></small></p>
        </div>

        <div class="btn-group pull-right">
            <% if comment.user == current_user -%>
                <%= link_to 'Destroy', comment, method: :delete, data: { confirm: 'Are you sure?' }, class: "btn btn-sm btn-default" %>
            <% end %>
        </div>
    </div>
<% end %>

Any clues?

Thanks

Routes.rb

Rails.application.routes.draw do
  resources :comments

  devise_for :users

  resources :links do
    member do
      get "like",    to: "links#upvote"
      get "dislike", to: "links#downvote"
    end
    resources :comments
  end
  root "links#index"

Upvotes: 1

Views: 98

Answers (2)

Rajarshi Das
Rajarshi Das

Reputation: 12320

Check your routes

It should be

  resources :links do
    member do
      get "like",    to: "links#upvote"
      get "dislike", to: "links#downvote"
    end
  end

  resources :comments

and also

Make sure you have <%= javascript_include_tag 'application' %> somewhere in you layout

Now it should work

Upvotes: 1

Sander Garretsen
Sander Garretsen

Reputation: 1723

Your method: :delete is not working. Adding method: :delete should change your link to make a DELETE request, but is looks like it's performing a GET request.

Rails has some javascript magic for changing the request type. Did you include

//= require jquery
//= require jquery_ujs

in your application.js?

Upvotes: 1

Related Questions