Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Index route not being generates by resource command in routes.rb file

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.

like image 313
Obromios Avatar asked Dec 08 '22 18:12

Obromios


2 Answers

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
like image 199
Gerry Avatar answered Apr 30 '23 04:04

Gerry


It should be resources instead of resource

like image 39
Tushar Kulkarni Avatar answered Apr 30 '23 05:04

Tushar Kulkarni