Jeremy Perry
Jeremy Perry

Reputation: 29

Resources are not creating routes. Can only manually route in my rails app

Rails.application.routes.draw do
  resources :users, only: [:index]
end

This is how I have my routes set up in my project. However it is not getting the route and I get an error:

No route matches [GET] "/index"

However my code below gives me no issues

Rails.application.routes.draw do
  get "/index", to: "users#index"
  #resources :users, only: [:index]
end

I am not sure what I am doing wrong. My controller and view files are set up properly, it just won't let me use resources for my routes. Any suggestions?

Upvotes: 0

Views: 881

Answers (1)

Semih Arslanoglu
Semih Arslanoglu

Reputation: 1139

This particular code is

Rails.application.routes.draw do
  resources :users, only: [:index]
end

generating a route for you with rails convention. By convention I mean that you have to send a GET request to /users in order to work. If you want to get users with /index you should use the second chunk of code you provided.

Basically

Rails.application.routes.draw do
  get "/index", to: "users#index"
end

is rerouting your index request to user index, and just telling rails to where to find it.

Upvotes: 3

Related Questions