Created by Gosia Ksionek
File allowing to define URLs for the application and map them to controllers.
Mapping single URL to single controller action using basic HTTP actions.
get '/patients/:id', to: 'patients#show'
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'
Allows to declare route for single resource - no need to use an ID in URL.
resource :photo
Allows to organize both controllers and URLs under common namespace.
namespace :admin do resources :articles, :comments end
Mapping path "/articles" to Admin::ArticlesController.
scope module: 'admin' do resources :articles, :comments end
resources :articles, module: 'admin'
Mapping path "admin/articles" to ArticlesController.
scope '/admin' do resources :articles, :comments end
resources :articles, path: '/admin/articles'
Allows to capture relationships define in models in routing.
resources :magazines do resources :ads end
Allows to capture relationships define in models in routing.
resources :magazines do resources :ads endResources should never be nested more than 1 level deep.
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]
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
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
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
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
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
It routes :controller and :action to right controlle and the rest is consider as part of params.
get ':controller/:action/:id/:user_id'
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 option allows to enforce a format for a dynamic segment.
get 'photos/:id', to: 'photos#show', constraints: { id: /[A-Z]\d{5}/ }
Defines request-based constraint.
get 'photos', to: 'photos#index', constraints: { subdomain: 'admin' }
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'.
Redirect one route to another.
get '/stories', to: redirect('/articles')