In this quick blog I would be showing you a few different ways and command to inspects routes within a Ruby on Rails Applications.
What's a Route, You might ask ?
Well, a Rails App come build in with a routing system that allow it to check the URLs of the request being made and then take an action that I should take to respond to that request. We can specify rules that the router will follow as configuration on a file under the
config/routes.rb
inside the app directory.Here an examples Routes file.
Rails.application.routes.draw do
# the priority is based upon order of creation: first created -> highest priority.
# For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
# Root route
root 'sessions#home'
# Custom routes here above the default resourceful routes
get '/signup', to: 'users#new', as: 'signup'
post '/signup', to: 'users#create'
get '/login', to: 'sessions#new', as: 'login'
post '/login', to: 'sessions#create'
delete '/logout', to: 'sessions#destroy', as: :logout
# OAuth2 OmniAuth callbacks routes
# Using math to avoid conflicts with other routes that may be using the same callback URL
match '/auth/:provider/callback', to: 'sessions#create', via: [:get, :post]
# Include Nested resources ( show or Index ) and ( new ): Make sure These routes forms can display validations errors.
resources :bookmarks do
resource :tags, only: [:index, :new]
end
resources :tags
resources :users, only: [:show] do
resources :bookmarks, only: [:index]
end
Command to View Routes
-
The most basic way to view the rout would be to use the rails routes command in the terminal =>
rails routes
orbin/rails routes
.
Prefix Verb URI Pattern Controller#Action
root GET / sessions#home
signup GET /signup(.:format) users#new
POST /signup(.:format) users#create
login GET /login(.:format) sessions#new
POST /login(.:format) sessions#create
logout DELETE /logout(.:format) sessions#destroy
GET|POST /auth/:provider/callback(.:format) sessions#create
new_bookmark_tags GET /bookmarks/:bookmark_id/tags/new(.:format) tags#new
bookmarks GET /bookmarks(.:format) bookmarks#index
POST /bookmarks(.:format) bookmarks#create
new_bookmark GET /bookmarks/new(.:format) bookmarks#new
edit_bookmark GET /bookmarks/:id/edit(.:format) bookmarks#edit
bookmark GET /bookmarks/:id(.:format) bookmarks#show
PATCH /bookmarks/:id(.:format) bookmarks#update
PUT /bookmarks/:id(.:format) bookmarks#update
DELETE /bookmarks/:id(.:format) bookmarks#destroy
tags GET /tags(.:format) tags#index
POST /tags(.:format) tags#create
new_tag GET /tags/new(.:format) tags#new
edit_tag GET /tags/:id/edit(.:format) tags#edit
tag GET /tags/:id(.:format) tags#show
PATCH /tags/:id(.:format) tags#update
PUT /tags/:id(.:format) tags#update
DELETE /tags/:id(.:format) tags#destroy
user_bookmarks GET /users/:user_id/bookmarks(.:format) bookmarks#index
user GET /users/:id(.:format) users#show
-
Another Flag you can use after the rails route command is
--expanded
so likerails route --expanded
output:
--[ Route 1 ]------------------------------------------------------------------------------------------------------------------------------------------------------------
Prefix | root
Verb | GET
URI | /
Controller#Action | sessions#home
--[ Route 2 ]------------------------------------------------------------------------------------------------------------------------------------------------------------
Prefix | signup
Verb | GET
URI | /signup(.:format)
Controller#Action | users#new
--[ Route 3 ]------------------------------------------------------------------------------------------------------------------------------------------------------------
Prefix |
Verb | POST
URI | /signup(.:format)
Controller#Action | users#create
--[ Route 4 ]------------------------------------------------------------------------------------------------------------------------------------------------------------
Prefix | login
Verb | GET
URI | /login(.:format)
Controller#Action | sessions#new
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
According to the Rails Documentations, you can search through the routes with the greg option:
-g
.Examples from rails docs of the command:
bin/rails routes -g new_comment
bin/rails routes -g POST
bin/rails routes -g admin
Another options is to use the option:
-c
to filter out the output to a specific controller.Another examples from rails docs of the command:
bin/rails routes -c users
bin/rails routes -c admin/users
bin/rails routes -c Comments
bin/rails routes -c Articles::CommentsController
Note: This is a Quick blog written to improve my Technical writing skill after finding an amazing community of Dev and Technical Writer Called Inked-In Founded By the ⭐️ React Developer @Elixir_js go give him a follow.
Feel Free to Follow me on Twitter as Well @eulis01 I be Tweeting later to the #100WordsADay #inkedIn Tags Thanks For Reading, See You Hacking around the Terminal.
Top comments (0)