Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@strzibny
Created May 10, 2016 18:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save strzibny/32ed2a8a1dd7b720034119ff5a143a58 to your computer and use it in GitHub Desktop.
Save strzibny/32ed2a8a1dd7b720034119ff5a143a58 to your computer and use it in GitHub Desktop.
Routing with Journey
#!/usr/bin/env ruby
require 'rails'
# App definition
class Application < Rails::Application
end
# Different application with directly defined routes that we can route to as well
class DifferentApplication < Rails::Application
routes.draw do
# We can respond with any Rack application, be it Rails controller or plain Proc
root to: Proc.new { [200, [], ["Hello from DifferentApplication"]] }
end
end
# Our routes definition
#
# The controllers and actions expected by Journey (the router) doesn't have to exist,
# it's just a definition and Rails won't complain.
Application.routes.draw do
# We can draw out routes directly with get, post, put/update and delete methods.
# They correspond with HTTP verbs and allow us to specify controller and action
# pair as well as path. Parameter to: can parse the controller and action for us
# from strings formatted as 'controller_name#action_name'. The path is optional.
# signup GET /signup(.:format) users#new
get 'signup', controller: :users, action: :new, as: 'signup'
# update_session PATCH /sessions/:id(.:format) sessions#update
patch 'sessions/:id', to: 'sessions#update', as: 'update_session'
# Alternatively we can easily define routes for our resources with simple
# resources definition. We can then adjust the scope by specifying only
# the relevant actions using only: parameter. Otherwise :index, :show, :new,
# :create, :edit, :update and :destroy actions will be used. Resources
# definition can be extended by any other routes within a block.
#
# user_profile GET /users/:user_id/profile(.:format) users#profile
# edit_user GET /users/:id/edit(.:format) users#edit
# user PATCH /users/:id(.:format) users#update
# PUT /users/:id(.:format) users#update
resources :users, only: [:edit, :update] do
# user_profile GET /users/:user_id/profile(.:format) users#profile
get 'profile', to: 'users#profile', as: 'profile'
end
# We can namespace or scope our routes. Scoped resources don't include
# the namespace part (/admin) but still route controllers scoped as Admin::.
namespace :admin do
resources :articles, :comments
end
mount DifferentApplication
# root will respond to document root path '/'
# Routes for DifferentApplication:
# root GET / #<Proc:0x0055bf94cb4a40@routing.rb:12>
root to: DifferentApplication
end
app_routes = Application.routes.routes
# RoutesInspector for Application.routes.routes. The same one (with ConsoleFormatter)
# is used when running rake routes (-g PATTERN or -c CONTROLLER) or (with HtmlTableFormatter)
# for displaying the routes in the browser on routing errors.
require 'action_dispatch/routing/inspector'
inspector = ActionDispatch::Routing::RoutesInspector.new(app_routes)
# We can filter routes by +controller+ named param or by pattern, e.g.:
# routes_filter = 'users'
routes_filter = nil
puts inspector.format(ActionDispatch::Routing::ConsoleFormatter.new, routes_filter)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment