launchscout / nku

NKU Class Spring
5 stars 14 forks source link

Logout not being properly referenced #82

Closed camdixon closed 10 years ago

camdixon commented 10 years ago

When a user is logged in, sometimes I get an error: No route matches [GET] "/users/1/logout"

So when I am logged in and the URL is showing /users my logout button works. When I am under a particular user such as /users/1/edit the logout button throws the error. Clearly my routes.rb file does not know it's there. How can I fix this?

My routes file looks like this

DanceTime::Application.routes.draw do
  get "welcome/index"
  get 'login', to: 'sessions#new', as: 'login'
  post 'login', to: 'sessions#create'
  get 'logout', to: 'sessions#destroy', as: 'logout'

  # The priority is based upon order of creation: first created -> highest priority.
  # See how all your routes lay out with "rake routes".

  resources :users
  resources :sessions

  # You can have the root of your site routed with "root"
  root 'welcome#index'

Here is the code to the view where it does not work.

<div class="page-header">
  <div class="row">
    <h1 class="col-sm-8" style="margin-left: 10px"><%= link_to @user.name, edit_user_path(@user) %></h1>
    <div class="col-sm-3">
      <span style="margin-right: 20px"><%= link_to 'Logout', 'logout', class: 'btn btn-default pull-right' %></span>
      <span style="margin-right: 20px"><%= link_to 'Home', users_path, class: 'btn btn-default pull-right' %></span>
    </div>
  </div>
</div>
mitchlloyd commented 10 years ago

If you specify a bare path like "logout" it is considered a relative URL. If you specify it as "/logout" it should always work. Better yet, use whatever URL helpers you have from the router. I bet you have a logout_path method that would work. Then you can stay a little more separated from your exact URLs.