Routes in ruby are already confusing for a “new to ruby” person like myself. All my understanding of the fundamentals (read from http://guides.rubyonrails.org/routing.html) went for a walk when I saw the following in routes.rb file in one of the projects:
But the controller, PhotosController only contained definition for show.
Where are the other methods???
As usual http://stackoverflow.com/ came to the rescue and Miguel, Rajesh and Oliver provided the reasoning.
This note is to summarize the discussion on the thread here: http://stackoverflow.com/questions/18699721/rails-resource-controllers.
When some one defines a resource in routes.rb using the syntax above, Rails created the following routes for the 7 HTTP methods:
If we want our application to take action on these method requests then we got to define these in the corresponding controller class, PhotosController.
In cases where the route is defined using [resources :photos] and one or more of these action methods are not defined in the controller, Rails will throw an exception (code 500).
So what is the correct way to define only the action methods that we will use. As mentioned by Oliver/Miguel in the stackoverflow.com thread use definition like one of the following when defining the route:
resources :photos, only: [:show, :index, <other action methods you NEED>]
resources :photos, except: [:create, :new, <other action methods you DO NOT NEED>]