- What is the purpose of the router in a Rails project?
- The router looks at the HTTP verb (GET, POST, PUT, or DELETE) and the URL that is being requested, and matches it with the appropriate controller action to run.
- What routes would be provided to you with the line
resources :items
?
get "/items" => "items#index"
get "/items/:id" => "items#show"
get "/items/new" => "items#new"
post "/items" => "items#create"
get "/items/:id/edit" => "items#edit"
put "/items/:id" => "items#update"
delete "/items/:id" => "items#destroy"
- What does
root :to => "items#index"
represent? How would you access that route in a web app?
- It represents the root of the web app. It defines which controller ('items') and action ('index') to map the route to. So when you navigate to http://www.mywebapp.com, you will be taken to the app's root/index page.
- What rake task is useful when looking at routes, and what information does it give you?
rake routes
outputs all of the routes your application knows.
- How would you interpret this output:
items GET /items(.:format) items#index
- The name of the route is
items
. The route itself is the incoming HTTP verbGET
to the path called/items
, which may contain a format (e.g., .doc), and the controller action it maps to isindex
(the controller-action combination isitems#index
).
- What is one major similiarity between Rails routing + controllers and the Sinatra projects we've been building?
- The routes and controllers are all the same. For example,
GET
requests to the app root display the app's index/dashboard.
- What is one major difference between Rails routing + controllers and the Sinatra projects we've been building?
- Rather than setting up the controller to direct the paths manually, they are already set up for us.