Search code examples
javascriptruby-on-railsrubydeviseruby-on-rails-3.2

Rails app cannot find 'destroy_user_session_path' in javascript


I have a rails app that has been migrated over the years starting at 2.2 to 3.2. I am using Devise, and Omniauth to get logins working, unfortunately I think during migration I have messed something up.

When I try to setup the session delete in application.html.erb I get the following error:

undefined local variable or method `destroy_user_session_path' for #<#<Class:0xb468e278>:0xb423e1dc>

application.html.erb:

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <meta http-equiv="content-type" content="text/html;charset=UTF-8" />
  <title>Coffee Tracker</title>
  <%= stylesheet_link_tag 'application'%>
  <!--%= stylesheet_link_tag 'scaffold' %-->
</head>
<!--%= javascript_include_tag :defaults %-->
<%= javascript_include_tag 'application' %>
<%= csrf_meta_tag %>
...snip...
<% if user_signed_in? %>
  <span id="currentuser"><%= current_user.email %></span>
  <%= link_to('Logout', destroy_user_session_path, :method => 'delete') %>
<% else %>

I also get the same error when I try the line:

 <%= link_to('Logout', destroy_user_session_path, :method => :delete) %>

application.js

// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults
//= require_self
//= require_tree .
//= require jquery
//= require jquery_ujs

Rails 3.2.0 ruby 1.8.7 (2011-02-18 patchlevel 334) [i686-linux]

  • rails (3.2.0)
  • jquery-rails (2.0.0)
  • devise (2.0.0)
  • omniauth (1.0.2)
  • omniauth-facebook (1.2.0)
  • omniauth-oauth2 (1.0.0)

Update: Here is the rake routes:

user_omniauth_callback     /users/auth/:action/callback(.:format) users/omniauth_callbacks#(?-mix:facebook)
           coffee_list GET /coffee/list(.:format)                 coffee#list
                  root     /                                      menu#index
                           /:controller(/:action(/:id(.:format))) :controller#:action

Solution

  • I was missing the the database authenticatable devise module which adds the route.

    app/models/user.rb:

     devise :omniauthable, :database_authenticatable
    

    rake routes:

          new_user_session GET    /users/sign_in(.:format)               devise/sessions#new
              user_session POST   /users/sign_in(.:format)               devise/sessions#create
      destroy_user_session DELETE /users/sign_out(.:format)              devise/sessions#destroy
    user_omniauth_callback        /users/auth/:action/callback(.:format) users/omniauth_callbacks#(?-mix:facebook)
               coffee_list GET    /coffee/list(.:format)                 coffee#list
                      root        /                                      menu#index
                                  /:controller(/:action(/:id(.:format))) :controller#:action
    

    I found documentation to confirm that shows the modules that use authenticatable create these routes in the devise_for documentation: http://rubydoc.info/github/plataformatec/devise/master/ActionDispatch/Routing/Mapper#devise_for-instance_method