Skip to content

Instantly share code, notes, and snippets.

@ltw
Last active December 11, 2015 16:58
Show Gist options
  • Save ltw/4631253 to your computer and use it in GitHub Desktop.
Save ltw/4631253 to your computer and use it in GitHub Desktop.
Fun with API Routes
# app/controllers/api/v1/users_controller.rb
class Api::V1::UsersController
def index
render json: { path: 'v1/users#index' }
end
def show
render json: { path: 'v1/users#show' }
end
end
# app/controllers/api/v1/posts_controller.rb
class Api::V1::PostsController
def index
render json: { path: 'v1/posts#index' }
end
end
# app/controllers/api/v2/users_controller.rb
class Api::V2::UsersController
def index
render json: { path: 'v2/users#index' }
end
end
>> api_users_path
=> '/api/v2/users'
>> api_user_path(user.id)
>> '/api/v1/users/:user_id'
>> api_v1_users_path
=> '/api/v1/users'
>> api_posts_path
=> '/api/v2/posts'
>> get '/api/v2/users'
=> "{'path': 'v2/users#index'}"
>> get '/api/v2/users/5'
=> "{'path': 'v1/users#show'}"
>> get '/api/v1/users'
=> "{'path': 'v1/users#index'}"
>> get '/api/v2/posts'
=> "{'path': 'v1/posts#index'}"
>> get '/api/users'
=> redirect_to '/api/v2/users'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment