Sankalp Singha
Sankalp Singha

Reputation: 4546

How to destroy comments in acts_as_commentable_on gem?

I have been trying to delete the comments that I have created with acts as commentable gem.

My code :

<%= link_to "×", comment_delete_place_path(comment.id), :method => :delete, :remote => true, :confirm => "Are you sure you want to remove this comment?", :disable_with => "×", :class => 'close' %>

Controller :

def comment_destroy
    if params[:comment]
      @comment = Comment.find(params[:id])
      if @comment.destroy
        respond_to do |format|
          format.html { redirect_to @place, notice: 'The comment was successfully deleted!'}
          format.js
      end
    end
  end
end

Routes :

delete 'places/comment/:id' => 'places#comment_destroy', as: 'comment_delete_place'

It gives me no error but, it does not delete the comment. Where am I going wrong?

Upvotes: 0

Views: 168

Answers (2)

Baldrick
Baldrick

Reputation: 24340

In your remote request, there is no comment parameter (only the id parameter is provided), so the following line is always false, you can remove it:

 if params[:comment]

Upvotes: 1

Max Williams
Max Williams

Reputation: 32933

link_to with a method anything other than GET is actually a bad idea, as links can be right clicked and opened in a new tab/window, and because this just copies the url (and not the method) it will break for non-get links. Also, links are clicked on by web page indexing spiders, and even though the links in question are probably only available to logged in users (and therefore not spiders), or have a 'confirm', it's still bad practise.

It's better to use button_to instead, which makes rails generate a form to produce the same result.

From a practical point of view buttons are better (for the above reasons) but they're also better from a conceptual point of view: generally speaking, links should "take you somewhere", whereas buttons should "do something". It's better to keep these two basic functionalities seperate.

Try a button_to (you'll need to check the syntax in your api). If it still doesn't work then check the url in the form, and compare it against the output from rake routes | grep comment

Upvotes: 1

Related Questions