20

I'm using devise sign_in and sign_up urls are working

but, when I try the url: http://localhost:3000/users/sign_out

it generates routing error

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

How can I fix this?

rake routes

rake routes
        new_user_session GET    /users/sign_in(.:format)           {:action=>"new", :controller=>"devise/sessions"}
            user_session POST   /users/sign_in(.:format)           {:action=>"create", :controller=>"devise/sessions"}
    destroy_user_session DELETE /users/sign_out(.:format)          {:action=>"destroy", :controller=>"devise/sessions"}
           user_password POST   /users/password(.:format)          {:action=>"create", :controller=>"devise/passwords"}
       new_user_password GET    /users/password/new(.:format)      {:action=>"new", :controller=>"devise/passwords"}
      edit_user_password GET    /users/password/edit(.:format)     {:action=>"edit", :controller=>"devise/passwords"}
                         PUT    /users/password(.:format)          {:action=>"update", :controller=>"devise/passwords"}
cancel_user_registration GET    /users/cancel(.:format)            {:action=>"cancel", :controller=>"devise/registrations"}
       user_registration POST   /users(.:format)                   {:action=>"create", :controller=>"devise/registrations"}
   new_user_registration GET    /users/sign_up(.:format)           {:action=>"new", :controller=>"devise/registrations"}
  edit_user_registration GET    /users/edit(.:format)              {:action=>"edit", :controller=>"devise/registrations"}
                         PUT    /users(.:format)                   {:action=>"update", :controller=>"devise/registrations"}
                         DELETE /users(.:format)                   {:action=>"destroy", :controller=>"devise/registrations"}
    status_message_index GET    /status_message(.:format)          {:action=>"index", :controller=>"status_message"}
                         POST   /status_message(.:format)          {:action=>"create", :controller=>"status_message"}
      new_status_message GET    /status_message/new(.:format)      {:action=>"new", :controller=>"status_message"}
     edit_status_message GET    /status_message/:id/edit(.:format) {:action=>"edit", :controller=>"status_message"}
          status_message GET    /status_message/:id(.:format)      {:action=>"show", :controller=>"status_message"}
                         PUT    /status_message/:id(.:format)      {:action=>"update", :controller=>"status_message"}
                         DELETE /status_message/:id(.:format)      {:action=>"destroy", :controller=>"status_message"}
                    home        /home(.:format)                    {:action=>"index", :controller=>"status_message"}
                    root        /                                  {:controller=>"home", :action=>"index"}

routes.rb

Microblog::Application.routes.draw do
  devise_for :users, :controllers => {:migrations => "users/registrations"}
  resources 'status_message'
  match 'home' => 'status_message#index'
  root :to => 'home#index'
end
Community
  • 1
  • 1
Sayuj
  • 6,836
  • 11
  • 54
  • 73

4 Answers4

45

The reason for the error is that the route is inaccessible using the GET HTTP method. Notice what the relevant line looks like in your rake routes output:

destroy_user_session DELETE /users/sign_out(.:format)

Meaning that, if you want to log the user out, you need to send a DELETE request to that url. In rails, you can generate a link that does that like so:

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

# alternatively (although NOT recommended):

link_to 'Sign out', '/users/sign_out', :method => :delete

The important part is :method => :delete. Note that a DELETE request is not really supported by browsers, rails is actually POSTing the data, but it sends a special parameter that simulates the DELETE method.

The reason behind this is that the "sign out" url is one that would log the current user out, a destructive action. If it was freely accessible through the browser, it could cause various problems. GET requests should never change the state of the server. For more information on this, here's a nice wikipedia article: http://en.wikipedia.org/wiki/REST#RESTful_web_services

Andrew Radev
  • 3,697
  • 19
  • 30
  • I've used ` :delete %>` but still it raising error message : `Routing Error No route matches [GET] "/users/sign_out"` – Sayuj Aug 08 '11 at 08:29
  • 2
    I forgot to mention that you need to have the "rails.js" javascript included on the page. When you click on that link, rails uses js to generate the POST request. You should put `` in the head section of the layout. – Andrew Radev Aug 08 '11 at 08:43
  • It gives Error `No route matches [GET] "/assets/rails.js"` Need I set any routing for assets? how? – Sayuj Aug 08 '11 at 09:07
  • 7
    Are you using the 3.1 version of rails? If that's the case, this wouldn't work like that. In 3.1, the "application.js" file in "app/assets/javascripts" should include the relevant file, and it's called "jquery_ujs.js". In this case, you should use `javascript_include_tag "application"` instead and you should be okay. You should definitely google around, though, this is still pretty new and you should try to understand the process. – Andrew Radev Aug 08 '11 at 10:42
  • @AndrewRadev Thanks a lot! `javascript_include_tag :application` does the trick for Rails3.1! – Matt Chuang Oct 10 '11 at 07:00
13

Any of the above suggestions did not work for me....

This post can be more useful to find other alternatives. No route matches "/users/sign_out" devise rails 3

I had to add following lines in the application.js file

//= require_tree .
// Add following lines.
//= require jquery 
//= require jquery_ujs
Community
  • 1
  • 1
Vishal
  • 2,769
  • 1
  • 13
  • 16
0

I had exactly the same symptoms, but I was also using jquery-turbolinks with masonry-rails to have images in the site transition and position "nicely".

I found that having this "broke" the transitions:

//= require jquery_ujs

so I removed it, and transitions worked like a charm... but when I went to log out, I got the above error even though my rake routes showed the existence of the path.

The "fix" for me turned out to be adding //= require rails-ujs and re-arranging my tree somewhat, so I ended up with:

//= require rails-ujs
//= require jquery
//= require jquery.turbolinks
//= require twitter/bootstrap
//= require masonry/jquery.masonry
//= require masonry/jquery.imagesloaded.min
//= require masonry/modernizr-transitions
//= require turbolinks
//= require_tree .

As an aside, anyone that has found this error while working in masonry should also add "transitions-enabled infinite-scroll clearfix" to the div with the id of the resource of what they want to operate on... in my case "bookads"

<div class="transitions-enabled" id="bookads">
  <% @books.each do |book| %>
    <div class="box panel panel-default">
      <div class="panel-heading index">
        <h3 class="panel-title text-center">
          <strong><%= book.title %></strong>
        </h3>
      </div>
     <div class="panel-body">... etc

Note the id of the individual elements I used is called "box"

In your coffeescript:

$ ->
  $('#bookads').imagesLoaded ->
    $('#bookads').masonry
      itemSelector: '.box'
      isFitWidth: true
      isAnimated: true

and to get "smooth" animation I also added masonry/jquery.imagesloaded.min and masonry/modernizr-transitions as shown in the tree above.

Hopefully this answer will save someone the few hours I spent looking for a solution.

srattigan
  • 511
  • 4
  • 12
0

Put this into app/views/layouts/application.html.erb

<%= stylesheet_link_tag    'application', media: 'all', 'data-turbolinks-track': 'reload' %>
<%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>

I hope it helps you