Tutorial by Examples

Routes are defined in config/routes.rb. They are often defined as a group of related routes, using the resources or resource methods. resources :users creates the following seven routes, all mapping to actions of UsersController: get '/users', to: 'users#index' post '/users', ...
You can filter what routes are available using constraints. There are several ways to use constraints including: segment constraints, request based constraints advanced constraints For example, a requested based constraint to only allow a specific IP address to access a route: constraints(...
Rails provides several ways to organize your routes. Scope by URL: scope 'admin' do get 'dashboard', to: 'administration#dashboard' resources 'employees' end This generates the following routes get '/admin/dashboard', to: 'administration#dashboard' post '/admin/empl...
To avoid repetition in nested routes, concerns provide a great way of sharing common resources that are reusable. To create a concern use the method concern within the routes.rb file. The method expects a symbol and block: concern :commentable do resources :comments end While not creating a...
You can perform redirection in Rails routes as follows: 4.0 get '/stories', to: redirect('/posts') 4.0 match "/abc" => redirect("http://example.com/abc") You can also redirect all unknown routes to a given path: 4.0 match '*path' => redirect('/'), via: :get ...
Defining a member block inside a resource creates a route that can act on an individual member of that resource-based route: resources :posts do member do get 'preview' end end This generates the following member route: get '/posts/:id/preview', to: 'posts#preview' # preview_post_p...
If you want to support a url parameter more complex than an id number, you may run into trouble with the parser if the value contains a period. Anything following a period will be assumed to be a format (i.e. json, xml). You can work around this limitation by using a constraint to broaden the accep...
You can add a home page route to your app with the root method. # config/routes.rb Rails.application.routes.draw do root "application#index" # equivalent to: # get "/", "application#index" end # app/controllers/application_controller.rb class Applicati...
resources :photos do member do get 'preview' end collection do get 'dashboard' end end This creates the following routes in addition to default 7 RESTful routes: get '/photos/:id/preview', to: 'photos#preview' get '/photos/dashboards', to: '...
If your application is available in different languages, you usually show the current locale in the URL. scope '/(:locale)', locale: /#{I18n.available_locales.join('|')}/ do root 'example#root' # other routes end Your root will be accessible via the locales defined in I18n.available_l...
mount is used to mount another application (basically rack application) or rails engines to be used within the current application syntax: mount SomeRackApp, at: "some_route" Now you can access above mounted application using route helper some_rack_app_path or some_rack_app_url. But ...
If you want to provide a URL out of convenience for your user but map it directly to another one you're already using. Use a redirect: # config/routes.rb TestApp::Application.routes.draw do get 'courses/:course_name' => redirect('/courses/%{course_name}/lessons'), :as => "course&quot...
If your routes file is overwhelmingly big, you can put your routes in multiple files and include each of the files with Ruby’s require_relative method: config/routes.rb: YourAppName::Application.routes.draw do require_relative 'routes/admin_routes' require_relative 'routes/sidekiq_routes' ...
If you want to add nested routes you can write the following code in routes.rb file. resources :admins do resources :employees end This will generate following routes: admin_employees GET /admins/:admin_id/employees(.:format) employees#index POST ...

Page 1 of 1