Skip to content

Instantly share code, notes, and snippets.

@Epigene
Last active January 19, 2016 10:16
Show Gist options
  • Save Epigene/ca7b219fcbc580160cf0 to your computer and use it in GitHub Desktop.
Save Epigene/ca7b219fcbc580160cf0 to your computer and use it in GitHub Desktop.
Creative routes guidelines
# required libraries always in first lines
require 'resque/server'
Rails.application.routes.draw do
# 1. External engines and services are mounted first
mount RailsAdmin::Engine => '/super_admin', as: 'rails_admin'
# 2. Always use "smart" redirects, conserving original parameters via http://stackoverflow.com/questions/16943169/rails-routes-how-to-pass-all-request-params-in-a-redirect
get '', to: redirect{ |_, request| ["/#{I18n.locale}", request.params.to_query].reject(&:blank?).join("?") }
# 3. root path is created by using the as: :root approach.
get '/:locale', to: 'programs#index', constraints: { locale: /../ }, as: :root
resources :api, only: :create
namespace :admin do
# 4. namespace "roots" are also created by using the as: :root approach.
get '/', to: 'chats#index', as: :root
# as: * is always placed last, via: *, if present, just before it.
match '/realtime/purchases/:id/dialed' => 'realtime#purchases_dialed', via: [:post, :patch], as: 'purchases_dialed'
resources :monitors do
collection do
get :users_archive
get :users_current
end
end
end
# mount namespaced controllers on routes without it (via http://stackoverflow.com/questions/3029954/difference-between-scope-and-namespace-of-ruby-on-rails-3-routing)
# assuming /controllers/secret_project/admin/blings_controller.rb
scope module: "secret_project" do
namespace :admin do
root to: 'blings#index'
resources :blings, only: :index do
get :custom
collection do
get :report
end
end
end
end
# 5. catch-all at the end
match "*path", to: redirect('/'), via: :all
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment