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.
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
It should be resources instead of resource
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With