1
votes

I know there are dozens of questions on different sites about this issue. I have tried a lot of things, and I still can't get the link with DELETE method to work.

I've created an empty rails application

Versions:

  • devise 2.0.4
  • rails: 3.2.2
  • Ruby: 1.9.2
  • RVM: 1.10.3

After I installed devise gem, I ran these commands:

rails generate devise:install
rails generate devise User

Fixes I have tried are below:

Initial link:

<%= link_to "Sign out", destroy_user_session_path %>

I added the delete method, in ticks and without them:

<%= link_to "Sign out", destroy_user_session_path, :method => 'delete' %>
<%= link_to "Sign out", destroy_user_session_path, :method => :delete %>

Added this to application.html.erb:

<%= javascript_include_tag :defaults %>

I uncommented these lines in assets/javascripts/application.js:

= require jquery
= require jquery_ujs

I also tried getting the GET link to work, changed this line from config/initializers/devise.rb

config.sign_out_via = : delete

to this:

config.sign_out_via = :get if Rails.env.test?

No matter what I try, DELETE links result in this:

No route matches [GET] "/users/sign_out" 

GET links don't work as well:

No route matches [GET] "/"

I have tried restarting the server, of course.

Rake routes contains this:

 destroy_user_session DELETE /users/sign_out(.:format)      devise/sessions#destroy

Thanks in advance.

3

3 Answers

8
votes

in devise.rb

find config.sign_out_via = :delete

change to config.sign_out_via = :get

if you use :get make sure you are using :method = :get and if you are using :delete using :method => :delete in your link

in production you may have to swap it round, so you should use an if statement to check RAILS_ENV

In your example you are probably running in development mode and not test, which is why it's probably not working.

And restart your server as it's an initialiser.

2
votes

This helper always works for me:

link_to "Logout", destroy_user_session_path, :method => :delete

Maybe you are not logged in properly? You could check with if current_user

0
votes

Can we see your routes file?

Specifically the devise_for call

You could also try setting the path explicitly

devise_for :users, path_names: { sign_out: 'sign_out' }