Routes
Using :path
to specify the URLs
# config/routes.rb
namespace :api, defaults: {format: 'json'} do
namespace :v1 do
resources :posts, only: :show do
resources :post_comments, path: 'comments'
end
end
end
# $rake routes
api_v1_post_post_comments GET /api/v1/posts/:post_id/comments(.:format) api/v1/post_comments#index {:format=>"json"}
POST /api/v1/posts/:post_id/comments(.:format) api/v1/post_comments#create {:format=>"json"}
new_api_v1_post_post_comment GET /api/v1/posts/:post_id/comments/new(.:format) api/v1/post_comments#new {:format=>"json"}
edit_api_v1_post_post_comment GET /api/v1/posts/:post_id/comments/:id/edit(.:format) api/v1/post_comments#edit {:format=>"json"}
api_v1_post_post_comment GET /api/v1/posts/:post_id/comments/:id(.:format) api/v1/post_comments#show {:format=>"json"}
PATCH /api/v1/posts/:post_id/comments/:id(.:format) api/v1/post_comments#update {:format=>"json"}
PUT /api/v1/posts/:post_id/comments/:id(.:format) api/v1/post_comments#update {:format=>"json"}
DELETE /api/v1/posts/:post_id/comments/:id(.:format) api/v1/post_comments#destroy {:format=>"json"}
api_v1_post GET /api/v1/posts/:id(.:format) api/v1/posts#show {:format=>"json"}
Show routes info
Display all routes
$ rake routes
Display part of routes using grep, good for wanting to list routes just match part of urls
$ rake routes | grep '/some_specific_url_part/'
Alternately, you could start your rails server ($rails s) and visit http://localhost:3000/rails/info/routes to see the all routes
Display restricted/specific routes
$ rake routes CONTROLLER=folder_name/controller_name
# example:
# Show Test::TestsController routes
$ rake routes CONTROLLER=test/tests
Convert routes info into an array
routes = Rails.application.routes.routes.to_a
match
TODO: ...
via
TODO: ...
constraints
TODO: ...
Reference
- Rails Routing from the Outside In
- How can I create a Rails 3 route that will match all requests and direct to one resource / page?
- change the URL without changing the resource name
- 4.7 Translated Paths
- Overriding the new and edit Segments
- 5.1 Listing Existing Routes
- Rails Named Routes: Path Vs. URL
- Can I rake the routes for a specific resource?
- Result of `rake routes` in ruby script
- Routing specs
- (quite hacky) to do routing specs(crawler_spec.rb)
- how get all routes in my rails application?