This is the 6th video in the Getting Started with Rails 7 series. In this video Mike sets up resourceful routes for articles. He updates the routing configuration for articles to use routing resources instead of specifying every action one by one. He goes through each of the RESTful routes produced by the articles resource and then updates the index to use the link_to
helper method and restores the ArticlesControllerTest to use article_url
.
This video covers:
- 00:00:10 Introduction
- 00:02:13 Explain the Article as a resource
- 00:03:47 Put
article_url
back in the ArticlesControllerTest - 00:04:21 Replace existing Article routing with resources :articles
- 00:04:51 An overview of the seven RESTful routes of the article resource
- 00:09:10 Examples of
article_path
andarticle_url
- 00:10:36 Using the Rails
link_to
helper with thearticle_path
or the record variable itself - 00:13:04 Review changes, commit, and push
In summary, here are the seven resourceful routes for articles and how they map to controller actions:
Controller method | Helper Method (_url or _path ) | Verb | URI Pattern |
index |
| GET | /articles(.:format) |
show | article_path(@article) | GET | /articles/:id(.:format) |
new | new_article_path | GET | /articles/new(.:format) |
create | articles_path | POST | /articles(.:format) |
edit | edit_article_path | GET | /articles/:id/edit(.:format) |
update | article_path(@article) | PATCH | /articles/:id(.:format) |
destroy | article_path(@article) | DELETE | /articles/:id(.:format) |
In some cases, controller actions will have the same helper method and URI pattern. In those cases, the verb of the action indicates how to route to the controller method. If you use GET
with article_path(@article) the router will send you to the show
action. However, if you use PATCH
you will end up in update
. Finally, if you use DELETE
it will call the destroy
action. Likewise, GET
will route to index and articles_path
POST
articles_path
will route you to create.
In development mode, you can type an incorrect URI pattern into the browser in a more readable format than calling rails routes
in the terminal.
The code for this series is open source and available on GitHub. View the commit for this video here.