5

In a ruby on rails app, I have the following routes file:

Rails.application.routes.draw do
  get 'pages/index'
  get 'pages/about'
  root to: 'pages#index'
  resource :graphs, only: [:index, :create, :show, :destroy]
end

This generates the routes for graphs#create, graphs#show, graphs#destroy but not for graphs#index. I think the reason for this is that when I generated the controller I used rails g controller Graph, i.e. I used the singular version instead of the plural of Graphs. To fix this I changed the corresponding file and directory names i.e. controller/graphs_controller.rb and views/graphs, and I changed the constant in graphs_controller.rb to GraphsController. Everything seems to work except for the index route not being generated. I can fix it by changing the resource declaration to

  resource :graphs, only: [:create, :show, :destroy] do
    get :index
  end

but this seems messy. It seems I need to change something else to make the transition from graph to graphs. How do I fix this? Note that I have restarted the server several times.

1

2 Answers 2

15

Use resources instead of resource:

Rails.application.routes.draw do
  get 'pages/index'
  get 'pages/about'
  root to: 'pages#index'
  resources :graphs, only: [:index, :create, :show, :destroy]
end
5

It should be resources instead of resource

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.