Skip to content

Instantly share code, notes, and snippets.

@snatchev
Last active December 16, 2015 07:09
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save snatchev/5396775 to your computer and use it in GitHub Desktop.
Save snatchev/5396775 to your computer and use it in GitHub Desktop.
DRYing up your Rails API routes. Rails' routing gives you the ability to add multiple contraints and scopes to routes. These conditions are and'ed together. Meaning all conditions must be true for the route to match. If you want to or them, to have any condition match, you are left with duplicated code. In this example, I wanted both http://api.…
Rails::Application.routes.draw do
def api_endpoints
controller :api do
resources :widget
post :register
get :help
end
end
constraints subdomain: "api" do
api_endpoints
end
scope "/api" do
api_endpoints
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment