Bruno Cruz
Bruno Cruz

Reputation: 83

Delete button is perfoming a show action instead of delete action in Rails 5.1

I have tried some solutions to this, but I cannot make it work properly. I am following Jordan Hudgens tutorial on Rails. I generated the "blog" structure via scafold, and initially everything was working properly. Now I can't find out what is going on. I am using Rails 5.1.

Every time I hit the "delete blog" button, it uses a GET action not a DELETE one.

When looking at the server console, I see it is perfoming a show action, not a delete one.

Server console log

Also, I reviewed my code and couldn't find any errors. Actually, I don't know where I could put debugging tools to find out the problem.

_blog.html.erb

<%= link_to 'Deletar Post', blog, method: :delete, data: { confirm: 'Deseja confirmar a exclusão do post?' } %>

blogs_controller.rb

  def destroy
    @blog.destroy
    respond_to do |format|
      format.html { redirect_to blogs_url, notice: 'Seu post foi excluído com sucesso.' }
      format.json { head :no_content }
    end
  end

Also, I think the routes are configured correctly.

Blog routes

My application.js file

//= require rails-ujs
//= require turbolinks
//= require_tree .
//= require jquery3
//= require popper
//= require bootstrap-sprockets
//= require jquery_ujs

Also, I have jquery-rails as a gem

gem 'devise', '~> 4.3'
gem 'bootstrap', '~> 4.0.0.beta2.1'
gem 'jquery-rails'
gem 'brunocruz_view_tool', '~> 0.1.0'
gem 'petergate', '~> 1.8'

It is odd, because the "delete" button was working properly, so I stopped testing it a while ago. Now I can't find what made it stop.

I also tried running the destroy action at the console. And it worked.

Destroy action using rails console

I have run the JavaScript console looking for errors. But I could not find any. (am I actually looking at the right place?)

In this test, I tried to run 2x this: click on "delete" button and then come back to blog index

I am totally lost here. I don't know what else I can do to find the error. Can you please help me?

Thanks,

Bruno

Upvotes: 2

Views: 1279

Answers (4)

Rabendra Sharma
Rabendra Sharma

Reputation: 36

Try with below syntax, hope this will help

<%= link_to 'Deletar Post', blog_path(blog), method: :delete, data: { confirm: 'Deseja confirmar a exclusão do post?' } %>

Upvotes: 0

MSC
MSC

Reputation: 3386

I have just spent all day on a similar issue and have resolved it. While the question's old, I thought I'd record this here for anyone else who's struggling with this as I imagine it will come up again.

The reason (in my case) the :delete method was being ignored is that UJS wasn't being sent to the browser. In the process of upgrading two apps (one from 4.2 and one from 5.1) to Rails 5.2 yesterday, I ended up with UJS going missing. I admit I ran numerous bundle upgrade and bundle installs and dithered over a few gems which I wasn't keen to use. Perhaps this created the problem for me.

It was only happening in my admin section where I had custom manifests for JS and CSS (admin.js and admin.css, respectively), which were correctly referenced in the layout and properly being precompiled as described in the official guide:

In app/views/layouts/admin.html.erb:

<%= javascript_include_tag 'admin', 'data-turbolinks-track': 'reload' %>

In app/assets/javascripts/admin.js:

//= require jquery
//= require rails-ujs
//= require_tree .

Mysteriously, there was no issue with the CSS manifest which was being served correctly.

On a side note, the change in my JS asset manifest (Rails 5.1: https://guides.rubyonrails.org/asset_pipeline.html) from

//= require jquery_ujs

to

//= require rails-ujs

(note the underscore's now a hyphen) was probably a bit of a red herring as I fiddled with those references to no avail.

The issue ended up being caused by an empty admin.coffee file in the same folder as my admin.js. The asset pipeline must have preferred to use that instead of the .js file of the same name. I don't have a need for CoffeeScript. Once I spotted that, and deleted it, I was back in business.

Upvotes: 2

E. D. B.
E. D. B.

Reputation: 51

Is this part of your code inside each? to pass a blog to delete you have to give one to him. <%= link_to 'Deletar Post', blog, method: :delete, data: { confirm: 'Deseja confirmar a exclusão do post?' } %>

example

<% @pops.each do |pop| %>
    #fields of pop, in your case is blog
    <%= link_to 'Destroy', pop, method: :delete, data: { confirm: 'Are you sure?' } %>
    #in the scaffold, the fields are generated inside a table, but it is not 
    #the line that passes a blog information that you want to delete, is
    #each. understood?
<% end %>

Upvotes: 0

Ronan Lopes
Ronan Lopes

Reputation: 3398

I believe there's an error with your path param on your link to:

<%= link_to 'Deletar Post', blog, method: :delete, data: { confirm: 'Deseja confirmar a exclusão do post?' } %>

That second param "blog" seems weird to me. Guess it would be blog_path or blog_url. Can you try that?

Upvotes: 0

Related Questions