Ruby on Rails: Routing May 1st, 2015

  • Routing connects URLs to code and generates paths from code
    // routes.rb ... e.g. directs a call of /patients/17 to the show action (method) in the patients controller with a parameter of {id: '17'} ... the 'as' option names the route, creating the patient_path and patient_url helper methods
    get '/patients/:id', to: 'patients#show', as: 'patient'
    
    // controller
    @patient = Patient.find(17)
    
    // view
    <%= link_to 'Patient Record', patient_path(@patient) %>
  • Resource routing allows you to quickly declare all of the common routes for a given resourceful controller (index, show, new, edit, create, update and destroy actions)
    resources :controller
    HTTP Verb Path Controller#Action Used for
    GET /photos photos#index display a list of all photos
    GET /photos/new photos#new return an HTML form for creating a new photo
    POST /photos photos#create create a new photo
    GET /photos/:id photos#show display a specific photo
    GET /photos/:id/edit photos#edit return an HTML form for editing a photo
    PATCH/PUT /photos/:id photos#update update a specific photo
    DELETE /photos/:id photos#destroy delete a specific photo
    • Creating a resourceful route will also expose a number of helpers to the controllers in your application…
      • photos_path returns /photos
      • new_photo_path returns /photos/new
      • edit_photo_path(:id) returns /photos/:id/edit (for instance, edit_photo_path(10) returns /photos/10/edit)
      • photo_path(:id) returns /photos/:id (for instance, photo_path(10) returns /photos/10)
  • Rail routes are matched in the order you specify
  • Singular resources map to plural controllers
  • You can group routes with namespacing
    namespace :admin do
     resources :articles, :comments
    end
  • You can have nested routes, but you shouldn’t go more than one level deep
    • shallow: true will help with clarity when nesting, only building routes with the minimal amount of information to uniquely identify the resource
  • You can match the verbs get, put, post, patch, and delete to a route with the via: option
  • Use * for wild cards in routes
  • You can redirect routes with the redirect helper
  • Specify the root of your app…
    root to: 'pages#main'
    root 'pages#main' # shortcut for the above
  • You can specify the controller for a path with the controller: option
    resources :photos, controller: 'images'
  • Get a list of your routes at http://localhost:3000/rails/info/routes or with rake routes in your terminal
  • Three built-in assertions for testing routes…
    • assert_generates … asserts that a particular set of options generate a particular path and can be used with default routes or custom routes
      assert_generates '/photos/1', { controller: 'photos', action: 'show', id: '1' }
      assert_generates '/about', controller: 'pages', action: 'about'
    • assert_recognizes … is the inverse of assert_generates, asserting that a given path is recognized and routes it to a particular spot in your application
      assert_recognizes({ controller: 'photos', action: 'show', id: '1' }, '/photos/1')
      assert_recognizes({ controller: 'photos', action: 'create' }, { path: 'photos', method: :post })
    • assert_routing … checks the route both ways
      assert_routing({ path: 'photos', method: :post }, { controller: 'photos', action: 'create' })

Resources