Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No route matches [GET] "/logout" [rails]

I get the following routing error when I click on "logout":

No route matches [GET] "/logout"

This is my application.html.erb file:

 <% if session[:user_id] %>
      <%= link_to 'Logout', logout_path, :method => :delete %>
    <% end %>

This is my routes.rb file:

get 'admin' => 'admin#index'
  controller :sessions do
    get 'login' => :new
    post 'login'=> :create
    delete 'logout' => :destroy
  end

  get "sessions/create"
  get "sessions/destroy"

Does anybody know how to solve this problem?

like image 546
Koin Arab Avatar asked Apr 29 '14 15:04

Koin Arab


2 Answers

change delete 'logout' => :destroy to get 'logout' => :destroy this will work.

like image 101
jimagic Avatar answered Oct 17 '22 07:10

jimagic


Have you enabled the built-in JavaScript libraries that are required (jquery/jquery_ujs)? The delete method is not directly supported by browsers, so this ends up actually creating a form with a hidden field _method, that Rails interprets to direct you to the correct place.

Edit:

To enable these libraries, check your application layout file. This is usually located at app/views/layouts/application.html.erb, but this may vary if you've customised your app.

You'll need to have the following tags in the head section:

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

That should get this working for you, but if not, post back and I'll try to help further. It'd be useful if you could get your code online - on GitHub or somewhere else - so that we can all see what you're trying to do in context.

like image 31
Steve Hill Avatar answered Oct 17 '22 06:10

Steve Hill