Akash Aggarwal Akash Aggarwal - 1 year ago 103
reST (reStructuredText) Question

Rails 5 app in production on heroku is calling sign out using method GET

I'm able to sign out easily on my localhost in development but I'm unable to sign out on production mode on heroku using devise. After going through logs I found out that

was being called with GET method.

This is the code generated by heroku server for logout button:

<a rel="nofollow" data-method="delete" href="/users/sign_out">Logout</a>

I made a custom route for the GET method to sign out but that isn't following the RESTful approach. Here is my routes file:

Rails.application.routes.draw do
resources :posts do
resources :post_comments
devise_for :users, controllers: {
sessions: 'users/sessions',
registrations: 'users/registrations'
devise_scope :user do
get 'users/sign_out', to: 'users/sessions#destroy'
root 'posts#index'


How do I fix this to use DELETE method?

Answer Source

As you have stated, Devise generates a sign out link that uses the delete method to take advantage of the right HTTP verb in a restful approach, however that must be accomplished by javascript. jquery-ujs reads the page body and creates an invisible form for each element with data-method attribute different from GET, captures the click event and submit that form with a "_method" attribute so that rails knows where to route it.

If anything prevents javascript from running, those forms aren't created and those links retain their natural behavior, which is to perform a GET request when clicked.

When something like that happens, it's important to make sure jquery and jquery-ujs are being loaded and that there isn't another piece of javascript raising errors and stopping all js execution before those forms are created. The easiest way to investigate is opening the browser inspector and reloading the page that contains the sign out link.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download