53

So let's say I have Posts and Comments and the url for show is /posts/1/comments/1. I want to create a link to delete that comment in the comments controller destroy method. How do I do that?

5 Answers 5

111

This answer is from 2009. It appears in Rails 7 (can't confirm, haven't worked in rails in 15 years) the correct answer using turbo can be found below.


<%= link_to 'Destroy', post_comment_path(@post, comment),
            data: {:confirm => 'Are you sure?'}, :method => :delete %>

in comments controller:

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

    respond_to do |format|
      format.html { redirect_to post_comments_path(@post) }
      format.xml  { head :ok }
    end
  end
Sign up to request clarification or add additional context in comments.

4 Comments

You also have to be sure that you have a <%= javascript_include_tag :all %> or similar tag in your header. Otherwise the link will be there but will not respect the :method => :delete. Just adding this comment because that was stumping me just now....
Quick note: javascript_include_tag :all is depricated in Rails > 3.1, the application layout defaults with = javascript_include_tag "application" which will accomplish the same thing
I ran into an issue where my AJAX links were logging users out. I ended up having to include the CSRF tags in the layout. More info here: stackoverflow.com/questions/6434283/… It's not entirely related to this actual question, but some people may run into this unexpectedly.
Updating your answer as per the answer below about "confirm: Are you sure?" to be included in data hash. I tried and verified it. It does need to be in the data hash.
11

Since some time ago, the confirm option has to be included in a data hash, otherwise it will be silently ignored:

<%= link_to 'Destroy',  post_comment_path(@post, comment),
    data: { confirm: 'Are you sure?' }, method: :delete %>

Comments

10

Given you have @post variable with your post object

Before Rails 7

<%= link_to 'Delete comment', post_comment_path(@post, comment),
  method: :delete, data: {confirm: 'Are you sure?'} %>

Rails 7 (with Turbo, out of the box)

<%= link_to 'Delete comment', post_comment_path(@post, comment),
  data: {turbo_method: :delete, turbo_confirm: 'Are you sure?'} %>

Comments

3

In rails 7.0.4.2

You can use destroy method this way 'turbo gem'

<%= link_to 'Delete', user, data: {turbo_method: :delete, turbo_confirm: 'Are you sure?'} %>

Turbo gives you the speed of a single-page web application without having to write any JavaScript. Turbo accelerates links and form submissions without requiring you to change your server-side generated HTML. It lets you carve up a page into independent frames, which can be lazy-loaded and operate as independent components. And finally, helps you make partial page updates using just HTML and a set of CRUD-like container tags. These three techniques reduce the amount of custom JavaScript that many web applications need to write by an order of magnitude. You can install 'turbo' in terminal by:

gem install turbo-rails -v 1.0.1

Or in Gemfile and bundled it.

gem 'turbo-rails', '~> 1.0', '>= 1.0.1'

Then

bundle install

1 Comment

Hopefully this comment get's upvoted to the top since this is the most up to date answer as of now.
1

Sometimes when you have <span>, <i> or nested elements inside of a <a> tag this way link_to use is difficult. You can inseted use raw HTML which is easy to handle, like so:

<a class="btn btn-sm" href="/blogs/<%[email protected]%>" data-method="delete">             
  <i class="pg-trash"></i><span class="bold">Delete</span>
</a>

1 Comment

You can still use link_to in these cases by passing a block to define the link name: <%= link_to @blog, method: :delete, class: "btn btn-sm" do %> <i class="pg-trash"></i><span class="bold">Delete</span> <% end %>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.