DEV Community

Cover image for Demystifying Ruby on Rails Routes for Beginners
Gonzalo Robaina
Gonzalo Robaina

Posted on

Demystifying Ruby on Rails Routes for Beginners

Routes in Rails determine how URLs are mapped to specific controller actions. When a user sends a request to your application, the routing system parses the URL and matches it to a controller action. This action is responsible for processing the request and returning an appropriate response.

Routes are defined in the config/routes.rb file within your Rails application.

Let's look at a simple example:

Rails.application.routes.draw do
  get 'welcome/index', to: 'welcome#index'
end
Enter fullscreen mode Exit fullscreen mode

In this example, we've created a route that maps a GET request to the welcome/index URL to the index action of the WelcomeController.

Resource vs Resources

Now that we understand the routing basics let's discuss the difference between resource and resources in Rails.

Resource

The resource method creates routes for a singular resource, such as a user profile or a shopping cart. This means it will only generate routes that do not require an ID to be passed in the URL. The generated routes will include new, create, show, edit, update, and destroy actions.

Here's an example:

Rails.application.routes.draw do
  resource :profile
end
Enter fullscreen mode Exit fullscreen mode

This will generate the following routes:

new_profile    GET    /profile/new      profiles#new
edit_profile   GET    /profile/edit     profiles#edit
profile        GET    /profile          profiles#show
               PATCH  /profile          profiles#update
               PUT    /profile          profiles#update
               DELETE /profile          profiles#destroy
               POST   /profile          profiles#create
Enter fullscreen mode Exit fullscreen mode

Resources

The resources method is used when you want to create routes for a collection of resources, such as blog posts or products. It generates routes for all seven default RESTful actions: index, new, create, show, edit, update, and destroy.

Here's an example:

Rails.application.routes.draw do
  resources :articles
end
Enter fullscreen mode Exit fullscreen mode

This will generate the following routes:

articles          GET    /articles          articles#index
new_article       GET    /articles/new      articles#new
edit_article      GET    /articles/:id/edit articles#edit
article           GET    /articles/:id      articles#show
                  POST   /articles          articles#create
                  PATCH  /articles/:id      articles#update
                  PUT    /articles/:id      articles#update
                  DELETE /articles/:id      articles#destroy
Enter fullscreen mode Exit fullscreen mode

Notice that in the resources method, the routes include an :id parameter, representing the specific resource's identifier.

Conclusion

Understanding Ruby on Rails routes is essential for any beginner developer working with Rails applications. By mastering the difference between resource and resources, you can create clean, RESTful routes that efficiently map user requests to the appropriate controller actions. Keep practicing, and soon you'll be a Rails routing pro!

Top comments (0)