Alan Kis
Alan Kis

Reputation: 1910

RoR routing error - No route matches [GET] "/articles/8/destroy"

I am folowing official Rails guide on guides.rubyonrails.org and I am stucked with following error:

No route matches [GET] "/articles/8/destroy" when I type path in browser address bar. Clicking on link in template file, doesn't delete article with passed id, just redirect again to it (article). Rails version is 4.2.1, database is sqlite and working environment is Windows 7.

Destroy method is as follows:

def destroy
    @article = Article.find(params[:id])
    @article.destroy

    redirect_to articles_path
  end

Rake routes output is:

articles_path       GET     /articles(.:format)             articles#index
                    POST    /articles(.:format)             articles#create
new_article_path    GET     /articles/new(.:format)         articles#new
edit_article_path   GET     /articles/:id/edit(.:format)    articles#edit
article_path        GET     /articles/:id(.:format)         articles#show
                    PATCH   /articles/:id(.:format)         articles#update
                    PUT     /articles/:id(.:format)         articles#update
                    DELETE  /articles/:id(.:format)         articles#destroy
root_path           GET     /                               welcome#index

I have checked for typos, even c/p all source, result is same. And ideas?

EDIT: added delete link

Delete link is as follows:

<%= link_to 'Destroy', article_path(article),
              method: :delete,
              data: { confirm: 'Are you sure?' } %>

Content of application.html.erb in views/layouts/ is like follows:

<!DOCTYPE html>
<html>
<head>
  <title>Blog</title>
  <%= stylesheet_link_tag    'application', media: 'all', 'data-turbolinks-track' => true %>
  <%= javascript_include_tag 'default', 'data-turbolinks-track' => true %>
  <%= csrf_meta_tags %>
</head>
<body>

<%= yield %>

</body>
</html>

javascript_include_tag 'default' is changed from 'application' to 'defaul', othervise I got coffeescript error.

Upvotes: 0

Views: 673

Answers (4)

Jesus Castello
Jesus Castello

Reputation: 1113

Route not found explanation

Your route says DELETE /articles/:id(.:format) articles#destroy. Which means it is expecting the HTTP DELETE method.

For /articles/8/destroy to work your route should be defined like this:

get '/articles/:id/destroy' => 'articles#destroy'

However, this is not the recommended way to go about it, instead make sure that you set the delete method in your delete link.

Manual testing

If you would like to test this manually you could try a tool like curl or the chrome plugin postman.

Both tools will let you specify the HTTP method to use. If you make a direct request via your browser all you are doing is sending a GET request.

Upvotes: 2

jljohnstone
jljohnstone

Reputation: 1230

Make sure you're including the Rails unobtrusive scripting adapter for jQuery in your application. Otherwise, links created with method: :delete won't be handled properly.

This is done via the JavaScript file jquery_ujs which is automatically included into your application's layout (app/views/layouts/application.html.erb) when you generated the application.

Upvotes: 0

MurifoX
MurifoX

Reputation: 15089

As this line states:

DELETE  /articles/:id(.:format)         articles#destroy

The server is expecting a DELETE request to trigger the destroy method in your controller. Something like this, from every guide on rails should do:

<%= link_to 'Destroy',  @article,  method: :delete, 
  data: { confirm: 'Are you sure?' } %>

The link_to helper with the delete method, creates a hidden form and submit it when clicked.

Upvotes: 0

Athar
Athar

Reputation: 3268

when you hit this /articles/8/destroy in your url, it is considered as the get request as you can see the error. No route matches [GET] "/articles/8/destroy"

but the DESTROY action accepts delete request in rails as you can see in the routes as well. now im not sure how you are triggering from the template but it should something like this considering your article is in object article

  <%= link_to "Delete", article, method: :delete, data: { confirm: 'Are you sure?' } %>  

Upvotes: 0

Related Questions