Defining RESTful Routes in Rails

Rails provides a powerful routing system that follows REST (Representational State Transfer) principles, making it easy to define predictable and maintainable routes for your application. For more on REST principles, check out our guide on what is REST and how does it relate to rails.

Understanding Rails Routing

Routes in Rails are defined in the config/routes.rb file, which maps incoming HTTP requests to controller actions. For more details, see our guide on understanding config routes rb file in rails.

Basic Route Definition

The most common way to define RESTful routes is using the resources method:

ruby
1Rails.application.routes.draw do
2 resources :articles
3end
4

This generates the following standard RESTful routes:

HTTP VerbPathController#ActionPurpose
GET/articlesarticles#indexList all articles
POST/articlesarticles#createCreate a new article
GET/articles/:idarticles#showShow a specific article
PATCH/PUT/articles/:idarticles#updateUpdate a specific article
DELETE/articles/:idarticles#destroyDelete a specific article
GET/articles/newarticles#newShow new article form
GET/articles/:id/editarticles#editShow edit form for article

For more on controllers and actions, see our guide on controllers in rails and their role.

Nested Resources

When you have resources that belong to other resources, you can use nested routes to represent these relationships:

ruby
1Rails.application.routes.draw do
2 resources :articles do
3 resources :comments
4 end
5end
6

This generates nested routes like:

  • GET /articles/:article_id/comments
  • POST /articles/:article_id/comments
  • GET /articles/:article_id/comments/:id

For more details, check out our guide on nested resources in rails routing.

Customizing Routes

Limiting Actions

You can limit which routes are generated using only or except:

ruby
1Rails.application.routes.draw do
2 resources :articles, only: [:index, :show]
3 resources :comments, except: [:destroy]
4end
5

Adding Custom Routes

Add custom member or collection routes for additional actions:

ruby
1Rails.application.routes.draw do
2 resources :articles do
3 member do
4 get 'preview'
5 post 'publish'
6 end
7
8 collection do
9 get 'search'
10 get 'featured'
11 end
12 end
13end
14

For more on MVC architecture, see our guide on mvc architecture in rails.

Route Constraints and Scoping

Route Constraints

Add constraints to routes to match specific patterns:

ruby
1Rails.application.routes.draw do
2 get 'users/:id', to: 'users#show',
3 constraints: { id: /[A-Z]\d{5}/ }
4end
5

Namespace and Scope

Organize routes using namespace and scope:

ruby
1Rails.application.routes.draw do
2 namespace :admin do
3 resources :articles
4 end
5
6 scope module: 'admin' do
7 resources :comments
8 end
9end
10

For more on API design, check out our guide on best practices high performing apis rails.

Best Practices

  1. Follow REST Conventions: Use standard RESTful routes when possible.
  2. Keep Routes Simple: Avoid deep nesting (more than 1 level).
  3. Use Resource Naming: Name your resources using plural nouns.
  4. Add Constraints: Use constraints to validate parameters.
  5. Organize Routes: Group related routes using namespaces or scopes.

Related Resources

Routing Basics

Advanced Routing

API Development

Conclusion

Defining RESTful routes in Rails is a fundamental aspect of building well-structured web applications. By following Rails conventions and REST principles, you can create maintainable and scalable routing systems. Remember to keep your routes simple, organized, and aligned with your application's domain model.

Suggested Articles