Skip to content

Getting Started with Rails 7 Episode 6: Resourceful Routes for Articles and the link_to Helper

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 and article_url
  • 00:10:36 Using the Rails link_to helper with the article_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 methodHelper Method (_url or _path)VerbURI Pattern
indexarticles_pathGET/articles(.:format)
showarticle_path(@article)GET/articles/:id(.:format)
newnew_article_pathGET/articles/new(.:format)
createarticles_pathPOST/articles(.:format)
editedit_article_pathGET/articles/:id/edit(.:format)
updatearticle_path(@article)PATCH/articles/:id(.:format)
destroyarticle_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 articles_path will route to index and 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.

Leave a Reply