doctopus
doctopus

Reputation: 5657

Why isn't this routes code rendering users index page?

I've completed the Hartl tutorial and managed to add some vanity URLs with the help of friendly_id and configuring the routes file. They use /username instead of /users/id

I did this by installing friendly_id and configuring routes.rb with the following:

  get '/users',      to: 'users#index'
  resources :users, path: '' do
    member do
      get :following, :followers
    end
  end

However, I want to make that code prettier, so I tried to change it to this:

  resources :users, except: :index, path: '' do
  member do
      get :following, :followers
    end
  end

But it doesn't render the /users index page! Am I writing this code wrong or am I going crazy?

EDIT:

Forgot to add, there's another resource :users further down. This is my entire routes.rb:

Rails.application.routes.draw do


  get 'password_resets/new'

  get 'password_resets/edit'

  root 'static_pages#home'
  get '/help',       to: 'static_pages#help'
  get '/about',      to: 'static_pages#about'
  get '/contact',    to: 'static_pages#contact'
  get '/signup',     to: 'users#new'
  post '/signup',    to: 'users#create'
  get '/login',      to: 'sessions#new'
  post '/login',     to: 'sessions#create'
  delete '/logout',  to: 'sessions#destroy'
  get '/users',      to: 'users#index'
  resources :users, path: '' do
    member do
      get :following, :followers
    end
  end

  resources :users
  resources :account_activations, only: [:edit]
  resources :password_resets,     only: [:new, :create, :edit, :update]
  resources :microposts,          only: [:create, :destroy]
  resources :relationships,       only: [:create, :destroy]
end

Upvotes: 1

Views: 42

Answers (2)

Sajan
Sajan

Reputation: 1923

You removed index and excluded it from resource.

Upvotes: 1

C dot StrifeVII
C dot StrifeVII

Reputation: 1885

The except: statement excludes the route for the index. It is effectively saying generate the resource routes for users except for the index action.

Try:

 resources :users, path: '' do
  member do
      get :following, :followers
    end
  end

Upvotes: 1

Related Questions