sturoid
sturoid

Reputation: 2721

Routing to show action on logout

I have a logout link that should be routing to users#do_logout but no matter what I do, if I click the link, it routes to the users#show. Here is the code:

Route:

  resources :users do
    member do
      get :profile
      post :profile
    end
    collection do
      get "signup", to: 'users#new'
      get "login"
      post "do_login"
      post "do_logout"
    end
  end

Link:

li = link_to "Sign Out", do_logout_users_path

Users controller action:

  def do_logout
    session[:user_id] = nil
    redirect_to :root
  end

Any help would be much appreciated. This is driving me insane.

Upvotes: 0

Views: 45

Answers (1)

Santhosh
Santhosh

Reputation: 29124

Your code is not working because, you have set up a POST route for do_logout and your Sign Out link is making a GET request.

To do a POST request from the view, you have to create a form

= form_tag do_logout_users_path do
  = submit_tag 'Sign Out'

OR

You could use delete method for this

in routes

delete "do_logout"

and the link

= link_to "Sign Out", do_logout_users_path, :method => :delete

Upvotes: 1

Related Questions