3

I am refactoring my access_controller into a sessions_controller and can't seem to get my destroy action working properly.

Logging in seems to work fine, but I am unable to log out of a session. Here is the link I have for logging out:

<%= link_to("Logout", :controller => "sessions", :action => 'destroy') %>

routes.rb

resources :sessions

sessions_controller.rb

class SessionsController < ApplicationController

  def new
  end

  def create
    ...
  end

  def destroy
    session[:user_id] = nil
    flash[:notice] = "You are now logged out"
    redirect_to root_url
  end
end

When I click "Logout" I get redirected to "/sessions/destroy" with a message of "The action 'show' could not be found for SessionsController". The destroy actions seems to want an id, but I don't need to pass in an id, I just want to run the action.

Tony Beninate
  • 1,926
  • 1
  • 24
  • 44
  • See http://stackoverflow.com/questions/2405635/how-to-empty-destroy-a-session-in-rails – junky Mar 03 '12 at 05:44
  • This shows me how to completely empty a session (which is interesting), but my problem is a routing problem. I'd like to be able to use routes here, but using 'resources :sessions' forces '/sessions/destroy' to look for an id. – Tony Beninate Mar 03 '12 at 05:55

1 Answers1

2

Ah, I found the answer here: http://railscasts.com/episodes/250-authentication-from-scratch

I need to set up my routes as follows:

get "log_out" => "sessions#destroy", :as => "log_out"
get "log_in" => "sessions#new", :as => "log_in"
resources :sessions
Tony Beninate
  • 1,926
  • 1
  • 24
  • 44
  • It worked for me... just changed `match 'ajax_logout', to: 'sessions#destroy', via: 'get'` TO `get 'ajax_logout' => 'sessions#destroy', :as=> 'ajax_logout'` and everything works ) Thanks for answer! – Elmor Mar 31 '13 at 09:19