3
votes

Seems like this is a very common question, and I have followed many of the fixes from google, yet my sign-out problem persists.

Here's what I have: Rails 4.1.1, Devise 3.2.4. Oh, btw, instead of User, I am calling my model AppUser

On view

As AppUser: <br>
<% if app_user_signed_in? %>
    Signed in as: <%= current_app_user.email %> <br>
    <%= link_to "Sign Out", destroy_app_user_session_path, method: :delete %>
<% else %>  
    <%= link_to "Sign Up", new_app_user_registration_path %> <br>
    <%= link_to "Sign In", new_app_user_session_path %>
<% end %>

/config/routes.rb

Rails.application.routes.draw do
  devise_for :app_users
  root 'home#index'
end

these are the partial routes from rake routes

                  Prefix Verb   URI Pattern                        Controller#Action
    new_app_user_session GET    /app_users/sign_in(.:format)       devise/sessions#new
        app_user_session POST   /app_users/sign_in(.:format)       devise/sessions#create
destroy_app_user_session DELETE /app_users/sign_out(.:format)      devise/sessions#destroy

on application.js

//= require jquery
//= require jquery_ujs
//= require_tree .

on application.html.erb, i included this as well

 <%= javascript_include_tag 'application' %>

and yet, i am still getting the get route error:

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

Why????

Ok. So, changing :delete to :get on /config/initializer/devise.rb fixes the problem, with method: :delete removed of course.

# The default HTTP method used to sign out a resource. Default is :delete.
config.sign_out_via = :get

But I still don't get it? Why? If I were to keep :delete on devise.rb, what was I missing?

========================

[SOLVED] I probably should edit the status update here:

I have finally figured out why. Even though i have set up my javascript, etc, correctly, but it didn't load because I stupidly inherit my controller class from "ActionController::Base" instead of "ApplicationController".

As I was working through my project, I noticed link_tos on other pages weren't able to send :delete method either.

Once I fixed the inheritance, they all work.

Gah.

3
that's weird, the only way i can recreate that error is to put the direction directly... can you post your routes file?Alexis
What does your config/routes.rb saypyRabbit
I added my routes.rb to my original post. Though the line concerning devise I believe is only 'devise_for :app_users', and that gave me a bunch of routes through rake routes, which partially I have included on the original post as wellobelus
this thing is driving me crazy. it must be something simple that I am missing. but i dont know what it is.obelus
this has been asked so many times....sevenseacat

3 Answers

0
votes

routes.rb

devise_scope :app_user do
  get "/app_users/sign_out" => "devise/sessions#destroy"
end
0
votes

JQuery?

If I were to keep :delete on devise.rb, what was I missing?

From the looks of it, the issue is likely to do with your system not rendering / processing :delete HTTP verbs correctly. The issue will be a Javascript one, as this http verb is created by the rails UJS on a standard get link

I would suggest you either have some problems with other javascript files on your system (check the browser console), or you are not loading the jquery_ujs file properly

The best way to test this will be to firstly look at the browser console, then see if you can bind an action to event inside your JS:

#app/assets/javascripts/application.js
$(document).on("click", "#test_link", function(){
    alert("test");
});
0
votes

I have finally figured out why. Even though i have set up my javascript, etc, correctly, but it didn't load because I stupidly inherit my controller class from "ActionController::Base" instead of "ApplicationController".

As I was working through my project, I noticed link_tos on other pages weren't able to send :delete method either.

Once I fixed the inheritance, they all work.

Gah.