Donald Walters

Software Engineer

Rails Routes

The routing on rails have a well rounded set of options. Creating individual, multiple routes, or all routes are equally easy to setup in the config/routes.rb file.

Rails breaks down CRUD into 7 possible routes: Create (New, Create), Read (Index, Show), Update (Edit, Update), Delete (Destroy)

Individual Routes in config/routes.rb (Manual)

Index (Read, all records)

get "/urlpath", to: "controllername#index"

New (init Creation new record, render new record form)

get "/urlpath/new", to: "controllername#new"

Create (Create record)

post "/urlpath", to: "controllername#create"

Show (Read a record)

get "/urlpath/:id", to: "controllername#show"

Edit (init Update record, render Update form)

patch "/urlpath/:id", to: "controllername#update"

Update (Update record)

put "/urlpath/:id", to: "controllername#update"

Delete (Delete record)

delete "/urlpath/:id", to: "controllername#destroy"

Display all routes (command console)

bin/rails routes
Multiple Routes

Create Multiple Routes (all 7 routes)

resources :controllername

Create Limited Multiple Routes - Example 1

resources :controllername [:index, :show]

Create Limited Multiple Routes - Example 2

resources :controllername [:show, :update, :destroy]
Multiple Resources

Multiple Controllers

resources :controller1name :controller2name :etc
Controller Namespaces

Namespace and Routing
Controller (controllername) is in the controllerdir and produces all routes; /controllerdir/controllername

namespace :controllerdir do     resources :controllername end

Scope
Controllers in controllerdir are accessed without /controllerdir/ being specified in the URL

scope module: "controllerdir" do     resources :controllername end
‐ or ‐
resources :articles, module: "admin"
Controller Namespaces

Namespace and Routing
Controller (controllername) is in the controllerdir and produces all routes; /controllerdir/controllername

namespace :controllerdir do
    resources :controllername
end