Akash Agarwal
Akash Agarwal

Reputation: 2520

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 users/sign_out 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
end
devise_for :users, controllers: {
    sessions: 'users/sessions',
    registrations: 'users/registrations'
}
devise_scope :user do
  get 'users/sign_out', to: 'users/sessions#destroy'
end
root 'posts#index'

end

How do I fix this to use DELETE method?

Upvotes: 2

Views: 356

Answers (3)

Travis Anderson
Travis Anderson

Reputation: 25

If anyone still having problem with this in 2017 rails 5? My solution

gem install

gem 'jquery-rails' run bundle then go to application.js in your assets and put this in... //= require jquery //= require jquery_ujs I just put them in the top pushed to heroku and tested and it passed good luck.

Upvotes: 0

Alexandre Angelim
Alexandre Angelim

Reputation: 6753

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.

Upvotes: 2

B Seven
B Seven

Reputation: 45943

 delete 'users/sign_out', to: 'users/sessions#destroy'

Upvotes: 0

Related Questions