Skip to content

Ruby on Rails - Routes

The Rails router, defined in config/routes.rb, is responsible for recognizing URLs and dispatching them to a controller’s action. It can also generate paths and URLs, avoiding the need to hardcode strings in your views and controllers.

Think of routes as a map for incoming requests. When a request arrives, the router consults this map to determine where the request should go.

Rails.application.routes.draw do
# Pattern 1 maps a URL to controller#action1
# Pattern 2 maps another URL to controller#action2
# ...
end

The most common way to define routes in Rails is by using resources. This helper declares standard RESTful routes for a given resource (e.g., books, articles).

For our library application with a BooksController, instead of defining each route manually, we can use:

config/routes.rb
Rails.application.routes.draw do
resources :books
# ... other routes ...
end

This single line resources :books automatically creates the following routes:

  • HTTP Verb | Path | Controller#Action | Named Helper
  • ------------|------------------|---------------------|-----------------
  • GET | /books | books#index | books_path
  • POST | /books | books#create | books_path
  • GET | /books/new | books#new | new_book_path
  • GET | /books/:id/edit | books#edit | edit_book_path(@book)
  • GET | /books/:id | books#show | book_path(@book)
  • PATCH/PUT | /books/:id | books#update | book_path(@book)
  • DELETE | /books/:id | books#destroy | book_path(@book)

These named helpers (like books_path, new_book_path) are what you use in your controllers and views (e.g., link_to 'New Book', new_book_path).

While resources is preferred for CRUD operations, you might need to define custom routes. Here’s how the routes from the original example would be defined more explicitly (though resources :books is better):

config/routes.rb
Rails.application.routes.draw do
get 'books', to: 'books#index', as: 'book_list' # Or simply 'books' if not using resources
get 'books/new', to: 'books#new', as: 'new_book_form'
post 'books', to: 'books#create' # For creating new books
get 'books/:id', to: 'books#show', as: 'show_book'
get 'books/:id/edit', to: 'books#edit', as: 'edit_book_form'
patch 'books/:id', to: 'books#update'
put 'books/:id', to: 'books#update' # Also common for updates
delete 'books/:id', to: 'books#destroy', as: 'delete_book'
# Example for a custom action like 'show_subjects'
# This might be better as a nested resource or in a SubjectsController
# get 'subjects/:id/books', to: 'books#show_subject_books', as: 'subject_books'
get 'books_by_subject/:subject_id', to: 'books#index', as: 'books_by_subject' # If filtering index
end
  • get 'books' maps a GET request to /books to the index action of BooksController.
  • as: 'book_list' creates a named helper book_list_path and book_list_url.
  • post 'books' maps a POST request to /books (typically from a new book form) to the create action.
  • patch 'books/:id' maps a PATCH request (typically from an edit book form) to the update action for a specific book ID.
  • delete 'books/:id' maps a DELETE request to the destroy action.

To set the default page for your application (e.g., when someone visits http://localhost:3000/), you define a root route:

config/routes.rb
Rails.application.routes.draw do
root 'books#index' # Makes the books index page the application root
resources :books
# ...
end

To see all the routes defined in your application, you can use the rails routes command in your terminal:

$ rails routes

This command lists all routes, including their HTTP verb, URI pattern, controller#action, and named helpers. It’s very useful for debugging routing issues or understanding the URL structure of an application.

Best Practice: Favor resources for standard CRUD interfaces. Use individual route definitions for custom actions or non-RESTful interfaces. Keep your routes.rb file clean and organized.

Further Reading: For more advanced routing techniques like nesting resources, member/collection routes, and constraints, refer to the official Rails Routing Guide: https://guides.rubyonrails.org/routing.html

With controllers handling requests and routes directing traffic, the next step is to create views that present data to the user and allow them to interact with the application.