2

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?

Akash Agarwal
  • 2,326
  • 1
  • 27
  • 57
  • Do you get any javascript errors when inspecting the page you use to sign out? Are jquery and jquery_ujs being required in your javascript? Maybe something went wrong during asset compilation that you couldn't pick up on your development environment. – Alexandre Angelim Sep 30 '16 at 22:30
  • @AlexandreAngelim I'm pretty sure the only error was that the page couldn't be found. Yes both of them are being used in application.js. I'm not sure how can I fix the asset compilation thingy, I'm new to rails – Akash Agarwal Sep 30 '16 at 22:59
  • Is the app public? Can you share the url? – Alexandre Angelim Sep 30 '16 at 23:05
  • There's a missing bootstrap dependency(Tether). Bootstrap is raising `Uncaught Error: Bootstrap tooltips require Tether`. That may be preventing js from executing down the road so the data-method links aren't dealt with properly. – Alexandre Angelim Oct 01 '16 at 01:00
  • http://stackoverflow.com/questions/34567939/how-to-fix-the-error-error-bootstrap-tooltips-require-tether-http-github-h – Alexandre Angelim Oct 01 '16 at 01:01
  • @AlexandreAngelim Thanks! It worked. Please post this as your answer :) – Akash Agarwal Oct 01 '16 at 12:27

3 Answers3

2

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.

Alexandre Angelim
  • 6,293
  • 1
  • 16
  • 13
0
 delete 'users/sign_out', to: 'users/sessions#destroy'
B Seven
  • 44,484
  • 66
  • 240
  • 385
  • Thanks but I tried that already and that doesn't work. Like I said in the question, heroku is picking the GET method by default. Please check this log generated by heroku`at=info method=GET path="/users/sign_out" ` – Akash Agarwal Sep 30 '16 at 21:29
0

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.