New to Rails, and following a tutorial that starts by creating the classic blog application.
Running: Rails 3.2.3 and Ruby 1.9.2
Started by creating a new Rails project, then generating scaffolding to create an Entry controller and views. Then used rake db:migrate to create the entries table on a mysql database server.
Started the Rails server (WEBrick 1.3.1), and pointed my browser to 0.0.0.0:3000/entries and it works fine, going to the index action by default.
But I was surprised to see that trying to point my browser to /entries/index returned an error. My entries controller includes the following actions: index, show, new, edit, create, update, destroy. Of these, only going to /entries/new actually works, the rest seem to be over-ridden by the :id default.
routes.rb contains two lines:
Mydiary::Application.routes.draw do
resources :entries
I thought the default format for accessing the actions of a controller was http://[hostname]/%5Bcontroller%5D/%5Baction%5D. So while it makes sense that not giving an action defaults to index, it frankly blew my mind that http://[hostname]/entries/index wouldn’t work. What possible reason could there be for this? Checking the rest it looks like only the new action works. All the rest are overridden by :id.
This doesn’t fit with what it says in the tutorial, by the way, which tells me this is new in Rails 3.
So I guess my question is, where can I find which actions are over-ridden by the :id action by default, and which aren’t? Very frustrating…
run
rake routesand you’ll see output that looks something like this:Basically, this results in:
GET /entries.POST /entriesGET /entries/newGET /entries/:id/editGET /entries/:idPUT /entries/:idDELETE /entries/:idA great place to start learning all this is the Rails Guide – Rails Routing from the Outside In.