Routes
Basic
Connect URLs to Code
- When your Rails application receives an incoming request for
GET /patients/17
- It asks the router to match it to a controller action. If the first matching route is
get '/patients/:id', to: 'patients#show'
- The request is dispatched to the
patientscontroller'sshowaction with{id: '17'}inparams - Rails uses snake_case for controller names here, if you have a multiple word controller like
MonsterTruckController, you want to usemonster_truck#showfor example
Configuring the Rails Router
- The router for your application or engine live in the file
config/routes.rband typically looks like this
Routes Controller Mapping
- Rails will map the route to its corresponding controller according to this syntax
resource :<resource_name> => <resource_name>Controller
- E.g:
resources :brands => BrandsController, :basket => BasketController - NOTES: the mapping only work if the controllers are directly under
app/controllersdirectory, for further customization, see [[Routes#Namespace | Namespace]]
Resource Routing: the Rails default
- Resource routing allows you to quickly declare all of the common routes for a given resourceful controller.
- A single call to
resourcecan declare all the necessary routes- index
- show
- new
- edit
- create
- update
- destroy
- A resourceful route provides a mapping between HTTP verbs and URLs to controller actions.

- NOTES: If. using
--apiwhen creating application, the routeindexandnewwill not be created (as we are dealing with [[Rails for API only (what I need) | API-only application]])
Singular Resource
- For example, you would like
/profileto always show the profile of the currently logged in user. In this case you can use a singular resource to map/profileto theshowactionget 'profile', to: 'users#show'
- Passing a
Stringtoto:will expect acontroller#actionformat. When using aSymbol, theto:option should be replaced withaction:. When using aStringwithout a#, theto:option should be replaced withcontroller:get 'profile', action: :show, controller: 'users'
Namespace
- You may wish to organize groups of controllers under a namespace.
- For example: you might group a number of administrative controllers under and
Admin::namespace, and place these controllers under theapp/controllers/admindirectory. You can route to such a group by using anamespace block
- This will create a number of routes for each of the
articlesandcommentscontroller. ForAdmin::ArticlesController, Rails will create
Nested Resources
- It's common to have resources that are logically children of other resources.
- E.g

- Nested routes allow you to capture this relationship in your routing. In this case, you could include this in route declartion


Adding More RESTful Actions
- We are not limited to the 7 routes that RESTful routing creates by default
- To add a member route, just add a
memberblock into the resource block
- This will recognize
/photos/1/previewwith GET, and route to thepreviewaction ofPhotosController, with the resource id value passed inparams[:id]