Last active
December 16, 2015 07:09
-
-
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.…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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