user3399101
user3399101

Reputation: 1537

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

So, I'm getting the below error when clicking on Sign Out on my drop down menu on the nav:

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

However, this only happens when using the sign out on the drop down nav (the hamburger menu for mobile devices) and not when clicking the sign out on the regular nav.

See the code below:

  <div class="container demo-5">
    <div class="main clearfix">
      <div class="column">
        <div id="dl-menu" class="dl-menuwrapper">
          <button class="dl-trigger">Open Menu</button>
          <ul class="dl-menu dl-menu-toggle">
            <div id="closebtn"  onclick="closebtn()"></div>
            <% if user_signed_in? %>
            <li><%= link_to 'FAQ', faq_path %></li>
            <li><a href="#">Contact Us</a></li>
            <li><%= link_to 'My Account', account_path %></li>                    
            <li><%= link_to 'Sign Out',  destroy_user_session_path, method: 'delete' %></li> <--- this is the line
            <% else %>
            <li><%= link_to 'FAQ', faq_path %></li>
            <li><a href="#">Contact Us</a></li>
            <li><%= link_to 'Sign In', new_user_session_path %></li>
            <li><%= link_to 'Free Trial', plans_path %></li>
            <% end %>
          </ul>
        </div><!-- /dl-menuwrapper -->
      </div>
    </div>
  </div><!-- /container -->
</div>

And this is the non-drop down code that works:

  <div class="signincontainer pull-right">
        <div class="navbar-form navbar-right">
          <% if user_signed_in? %>
            <%= link_to 'Sign out', destroy_user_session_path, class: 'btn signin-button', method: :delete %>
              <div class="btn signin-button usernamefont"><%= link_to current_user.full_name, account_path %></div>
          <% else %>
....rest of code here

Updated error:

ActionController::RoutingError (No route matches [GET] "/user/sign_out"):
  actionpack (4.0.4) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
  actionpack (4.0.4) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
  railties (4.0.4) lib/rails/rack/logger.rb:38:in `call_app'
  railties (4.0.4) lib/rails/rack/logger.rb:20:in `block in call'
  activesupport (4.0.4) lib/active_support/tagged_logging.rb:68:in `block in tagged'
  activesupport (4.0.4) lib/active_support/tagged_logging.rb:26:in `tagged'
  activesupport (4.0.4) lib/active_support/tagged_logging.rb:68:in `tagged'
  railties (4.0.4) lib/rails/rack/logger.rb:20:in `call'
  quiet_assets (1.0.2) lib/quiet_assets.rb:18:in `call_with_quiet_assets'
  actionpack (4.0.4) lib/action_dispatch/middleware/request_id.rb:21:in `call'
  rack (1.5.2) lib/rack/methodoverride.rb:21:in `call'
  rack (1.5.2) lib/rack/ru

Upvotes: 1

Views: 1166

Answers (3)

Richard Peck
Richard Peck

Reputation: 76774

@colinm's answer is right; but let me explain why:

--

HTTP Verbs

Devise sets a series of RESTful routes when you install it. Of al the routes it generates, there is destroy_user_session_path, which uses the delete HTTP Verb:

 destroy_user_session DELETE /users/sign_out   {controller:"devise/sessions", action:"destroy"}

This means this route is only available when you use the delete method in your link_to method, like this:

<%= link_to "Log Out", destroy_user_session_path, method: :delete %>

HTML defaults to the GET HTTP Verb, meaning if you don't explicitly define method: :delete in your code, Rails is not going to look for the right verb for the route

Upvotes: 1

colinm
colinm

Reputation: 4288

The method specified must be a symbol.

In the code that's not working, it's currently a string:

link_to 'Sign Out', destroy_user_session_path, method: 'delete' 

In the code that's working, it's correctly specified as a symbol:

link_to 'Sign out', destroy_user_session_path, method: :delete

Upvotes: 2

acacia
acacia

Reputation: 1387

Lets try this; Remove

devise_for :users, :path => 'user'

Add this;

devise_for :users, :path => 'user' do
  get '/user/sign_out' => 'devise/sessions#destroy' 
end

More; here

Last suggestion

I have found out that it requre javascript to turn the request into a DELETE;

So

use devise_for :users, :path => 'user' in your config/routes

Ensure that you have javascript loaded in application.html.erb

<%= javascript_include_tag "application" %>

Secondly make sure you have jquery added with jquery-ujs gem and with something like this;

//= require jquery
    //= require jquery_ujs

Upvotes: -1

Related Questions