Ruby rails: routes

From wikinotes

Routes define URL-endpoints, and which Controller/Action they get routed to.
You may also be interested in ruby rails: url helpers.

Documentation

routing docs https://guides.rubyonrails.org/routing.html

Tutorials

routing overview with examples https://medium.com/lightthefuse/rails-5-routes-cookbook-10-recipes-for-the-novice-rails-developer-and-beyond-9986f43064bc

Locations

{project}/config/routes.rb routes are configured here

Commands

rails routes   # print all application routes
Rails.application.routes.recognize_path("images/cats/1")  # get controller/action for URL path
Rails.application.routes.named_routes.each{|p,s| puts p,s} 
Rails.application.routes.url_helpers.my_path_helper

Example

Rails.application.routes.draw do
  root 'welcome#index'                      # domain.com               -> WelcomeController.index()
  get 'welcome/index'                       # domain.com/welcome#index -> WelcomeController.index()
  get 'welcome' => 'welcome#index'          # domain.com/welcome       -> WelcomeController.index()
  get '/patients/:id', to: 'patients#show'  # domain.com/patients/123  -> PatientsController.show()
end

Syntax

routes

Rails.application.routes.draw do
  root 'welcome#index'                      # domain.com               -> WelcomeController.index()
  get 'welcome/index'                       # domain.com/welcome#index -> WelcomeController.index()
  get 'welcome' => 'welcome#index'          # domain.com/welcome       -> WelcomeController.index()
  get '/patients/:id', to: 'patients#show'  # domain.com/patients/123  -> PatientsController.show()
end

resources

Basics

Resources let you model objects for CRUD operations on an object.

Rails.application.routes.draw do
  resources: :articles                      # build REST interface for CRUD objects

  # HTTP methods on 'articles' resource
  #               GET       /articles(.:format)          articles#index
  #               POST      /articles(.:format)          articles#create
  #               PATCH     /articles/:id(.:format)      articles#update
  #               DELETE    /articles/:id(.:format)      articles#destroy

  # Interactive Pages on 'articles' resource
  # new_article   GET       /articles/new(.:format)      articles#new
  # edit_article  GET       /articles/:id/edit(.:format) articles#edit
  # article       GET       /articles/:id(.:format)      articles#show

  # Deprecated HTTP methods
  #               PUT       /articles/:id(.:format)      articles#update
  #               GET       /                            welcome#index
end

They also add helper functions to controllers.

# resources: :photos

photos_path           #> /photos
new_photo_path        #> /photos/new
edit_photo_path(:id)  #> /photos/:id/edit   (for instance, edit_photo_path(10) returns /photos/10/edit)
photo_path(:id)       #> /photos/:id        (for instance, photo_path(10) returns /photos/10)

Members/Collections

Members/Collections let you define custom GET/PATCH/... methods on a resource (that wouldnt't be generated automatically).

  • member operates on a single resource GET /photos/1/preview
  • collection operates on multiple resources GET /photos/search
resources :photos do
  member do
    # GET /photos/1/preview -> PhotosController.preview()
    get 'preview'
  end

  collection do
    # GET /photos/search -> PhotosController.search()
    get 'search'
  end
end


Nested Resources

Resources can also be nested under each other.

todo

When testing a path to a nested-resource, you must include the ID of the resource above it in params.

class HouseControllerTest < ActionController::TestCase
  test "..." do
    # POST https://domain.com/family/1/house#create
    post(:create, params: { family: 1 }
  end
end

namespaces/scopes

Namespaces and Scopes are like subdirectories for your URL.

  • namespaces: both the URL and the controller will have a directory
  • scopes: only the URL will have a directory
namespace :factory do
  # GET /factory/cars(.:format) --> {project}/app/controllers/factory/cars_controller.rb
  resources :cars
end

scope :factory do
  # GET /factory/cars(.:format) --> {project}/app/controllers/cars_controller.rb
  resources :cars
end

route globbing

route globbing binds the remainder of the url to a URL param http://example.com?foo=bar.

# GET /rent/lisbon/suv-sedan
# params[:slugs] == 'suv-sedan'
get '/rent/*slugs', to: 'cars#index', as: :rent_cars