Skip to content

Instantly share code, notes, and snippets.

@petrblaho
Created June 21, 2012 11:40
Show Gist options
  • Save petrblaho/2965271 to your computer and use it in GitHub Desktop.
Save petrblaho/2965271 to your computer and use it in GitHub Desktop.
Rails routing for nested resources with :as parameter
api_post_comments GET /api/posts/:post_id/comments(.:format) comments#index
POST /api/posts/:post_id/comments(.:format) comments#create
new_api_post_comment GET /api/posts/:post_id/comments/new(.:format) comments#new
edit_api_post_comment GET /api/posts/:post_id/comments/:id/edit(.:format) comments#edit
api_post_comment GET /api/posts/:post_id/comments/:id(.:format) comments#show
PUT /api/posts/:post_id/comments/:id(.:format) comments#update
DELETE /api/posts/:post_id/comments/:id(.:format) comments#destroy
api_posts GET /api/posts(.:format) posts#index
POST /api/posts(.:format) posts#create
new_api_post GET /api/posts/new(.:format) posts#new
edit_api_post GET /api/posts/:id/edit(.:format) posts#edit
api_post GET /api/posts/:id(.:format) posts#show
PUT /api/posts/:id(.:format) posts#update
DELETE /api/posts/:id(.:format) posts#destroy
api_post_comments GET /api/posts/:api_post_id/comments(.:format) comments#index
POST /api/posts/:api_post_id/comments(.:format) comments#create
new_api_post_comment GET /api/posts/:api_post_id/comments/new(.:format) comments#new
edit_api_post_comment GET /api/posts/:api_post_id/comments/:id/edit(.:format) comments#edit
api_post_comment GET /api/posts/:api_post_id/comments/:id(.:format) comments#show
PUT /api/posts/:api_post_id/comments/:id(.:format) comments#update
DELETE /api/posts/:api_post_id/comments/:id(.:format) comments#destroy
api_posts GET /api/posts(.:format) posts#index
POST /api/posts(.:format) posts#create
new_api_post GET /api/posts/new(.:format) posts#new
edit_api_post GET /api/posts/:id/edit(.:format) posts#edit
api_post GET /api/posts/:id(.:format) posts#show
PUT /api/posts/:id(.:format) posts#update
DELETE /api/posts/:id(.:format) posts#destroy
Blog::Application.routes.draw do
# 1st variant
scope "/api", :as => 'api' do
resources :posts do
resources :comments
end
end
# 2nd variant
scope "/api" do
resources :posts, :as => 'api_posts' do
resources :comments
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment