Routes.rb – Short summary



Routes.rb – Short summary

0 0


routes_pres


On Github gpks / routes_pres

Routes.rb

Short summary

Created by Gosia Ksionek

routes.rb

File allowing to define URLs for the application and map them to controllers.

Basic HTTP actions

Mapping single URL to single controller action using basic HTTP actions.

get '/patients/:id', to: 'patients#show'

Resource

Instead of declaring separate routes for index, show, new, edit, create, update and destroy actions, a resourceful route declares them in a single line of code.

resources :photos
resources :photos, controller: 'images'

Single Resource

Allows to declare route for single resource - no need to use an ID in URL.

resource :photo

Namespace

Allows to organize both controllers and URLs under common namespace.

namespace :admin do
  resources :articles, :comments
end

Scope

Mapping path "/articles" to Admin::ArticlesController.

scope module: 'admin' do
  resources :articles, :comments
end
resources :articles, module: 'admin'

Scope

Mapping path "admin/articles" to ArticlesController.

scope '/admin' do
  resources :articles, :comments
end
resources :articles, path: '/admin/articles'

Nested Resources

Allows to capture relationships define in models in routing.

resources :magazines do
  resources :ads
end

Nested Resources

Allows to capture relationships define in models in routing.

resources :magazines do
  resources :ads
end
Resources should never be nested more than 1 level deep.

Nested Resources - Shallow

Generates shallow routes for nested resource(s).

This allows URLs for resources that otherwise would be deeply nested such as a comment on a blog post like /posts/a-long-permalink/comments/1234 to be shortened to just /comments/1234.

resources :posts, shallow: true do
  resources :comments
end
#Is the same as:

resources :posts do
  resources :comments, except: [:show, :edit, :update, :destroy]
end
resources :comments, only: [:show, :edit, :update, :destroy]

Collection

Allows to add additional paths to resources via GET.

resources :photos do
  collection do
    get 'search'
  end
end
resources :photos do
  get 'search', on: :collection
end

Member

Allows to add additional path to single resource via GET.

resources :photos do
  member do
    get 'preview'
  end
end
resources :photos do
  get 'search', on: :member
end

Resources additions

Different hacks to resources

resources :posts, path_names: { new: "brand_new" }
# The above example will now change /posts/new to /posts/brand_new

resources :posts, path: 'postings'
# The resource and all segments will now route to /postings instead of /posts

Defaults

Allows you to set default parameters for a route. Using this, the :id parameter here will default to 'home'.

defaults id: 'home' do
  match 'scoped_pages/(:id)', to: 'pages#show'
end

Concerns

Allows you to set default parameters for a route. Using this, the :id parameter here will default to 'home'.

concern :commentable do
  resources :comments
end
resources :messages, concerns: :commentable
resources :messages do
  resources :comments
end

Dynamic segments

It routes :controller and :action to right controlle and the rest is consider as part of params.

get ':controller/:action/:id/:user_id'

Match

Allows to use the match method with the :via option to match multiple verbs at once.

match 'photos', to: 'photos#show', via: [:get, :post]

Constraints

:constraints option allows to enforce a format for a dynamic segment.

get 'photos/:id', to: 'photos#show', constraints: { id: /[A-Z]\d{5}/ }

Constraints with subdomain

Defines request-based constraint.

get 'photos', to: 'photos#index', constraints: { subdomain: 'admin' }

Wildcard segments

Wildcard segments can occur anywhere in a route.

get 'books/*section/:title', to: 'books#show'

Code above would match books/some/section/last-words-a-memoir with params[:section] equals 'some/section', and params[:title] equals 'last-words-a-memoir'.

Redirections

Redirect one route to another.

get '/stories', to: redirect('/articles')
Routes.rb Short summary Created by Gosia Ksionek