No match for route [GET] "/ logout"?
Rails.application.routes.draw do
root to: 'users#index'
get '/login', to: 'sessions#new'
post '/login', to: 'sessions#create'
delete '/logout', to: 'sessions#destroy'
resources :users
end
<% if logged_in? %>
<li><%= link_to "Sign out", logout_path, method: :delete %>
<% end %>
GemFile
gem 'jquery-rails'
gem 'rails', '4.2.2'
gem 'turbolinks'
//= require jquery
//= require jquery_ujs
//= require turbolinks
//= require_tree .
Is this a javascript issue? It doesn't seem to want to recognize the :: delete method?
Here is my application.html.erb file:
<!DOCTYPE html>
<html>
<head>
<title>Workspace</title>
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
<%= csrf_meta_tags %>
</head>
<body>
<%= yield %>
</body>
</html>
Is there an alternative way to pass the :: delete route method?
source to share
While submitting applications with broken javascript is not a good idea, it might make sense to secure some critical aspects of your application, such as logging out, using button_to
instead link_to
.
button_to "foo", bar method: delete
and link_to "foo", bar method: delete
both achieve the same goal - the request is sent as a POST request with a special parameter _method
used Rack::MethodOverride
to set the request method to PATCH, DELETE, etc.
button_to
creates an actual form containing hidden inputs and one button. link_to
uses javascript (Rails UJS) to create a virtual form and submits the form when the user clicks on the link.
source to share
One of two things happens:
-
The presented element
<a>
does not contain an attributedata-method
to block UJS. Maybe there is an outdated version cached or you are looking at a different view? The easiest way to check is to go to your web inspector or view the source and confirm the existence of the attribute by clicking the corresponding link. -
UJS does not handle clicking on an element. It might be due to a JS error, but it might be another click handler that you registered with priority. Check your JS console for errors to eliminate this as a potential cause, and if not, make sure the UJS script is in the sources panel:
If it is present and the behavior is the same, try adding a breakpoint in functions
handleMethod
in UJS and running again. If this happens, go to find out what is happening that is causing the crash. If it doesn't start, you can step back from there and look at other handlers by reference.[ ]
source to share