Customizing Rails Routes
Customize the Router in Rails Application
There are different ways to customize Rails routes using various options which are described in this section. So before moving forward, let’s have a look about what options we are discussing in this section as below,
- Customize route helper using
:as
- Restrict action by using
:only
and:except
- Match route to have multiple actions using
:match
- Renaming routes URL using
:path
- Redirection of URL using
:redirect
Customize route helper using :as
There are few scenarios where Prefix for defined routes needs to get changed and the scenarios could be various but some of them as below,
- Shortening Prefix as
home_comment_review_path
tohc_review_path
as below,
config/routes.rbRails.application.routes.draw do
resources :homes
get ‘homes/comments_review_retweet’ => homes#retweet’
end
At terminal end,
$ rake routes | grep retweethomes_comments_review_retweet GET /homes/comments_review_retweet(.:format) reviews#retweet
Now, this will give Prefix for review as, homes_comments_review_retweet
which is too long and in case there is need of shorter name for it then it can be performed as,
config/routes.rbRails.application.routes.draw do
resources :homes
get ‘homes/comments_review_retweet’ => homes#retweet’, as: :retweet
end
Now the Prefix will become as,
$ rake routes | grep retweetretweet GET /homes/comments_review_retweet(.:format) reviews#retweet
- Another important case is, naming routes to have specific Prefix in the case as below,
Let’s define a route as below,
config/routes.rbRails.application.routes.draw do
resources :homes
get ‘homes/:id/reviews => ‘homes#reviews’
end
Now this will tend to give routes as below,
terminal=>$ rake routesPrefix Verb URI Pattern Controller#Action home GET /homes/:id(.:format) homes#show
PATCH /homes/:id(.:format) homes#update
PUT /homes/:id(.:format) homes#update
DELETE /homes/:id(.:format) homes#destroy
GET /homes/:id/reviews(.:format) homes#reviews
As you can see, there is no specific Prefix for ‘reviews’ method route(last line). To have some specific Prefix along with it can be given as,
config/routes.rbRails.application.routes.draw do
resources :homes
get ‘homes/:id/reviews’ => ‘homes#reviews’, as: :reviews
end
Now this will tend to give routes as below,
terminal=>$ rake routesPrefix Verb URI Pattern Controller#Actionhome GET /homes/:id(.:format) homes#show
PATCH /homes/:id(.:format) homes#update
PUT /homes/:id(.:format) homes#update
DELETE /homes/:id(.:format) homes#destroy
reviews GET /homes/:id/reviews(.:format) homes#reviews
Restrict action by using :only and :except
Now, in case of defining a route with specific actions involved in it and not like default with all actions. In this case options like :only
and :except
comes in the role as below,
config/routes.rbRails.application.routes.draw do
resources :homes, only: [:get, :post]
end
Then the routes available will be as below,
terminal=>$ rake routesPrefix Verb URI Pattern Controller#Actionhomes GET /homes(.:format) homes#index
POST /homes(.:format) homes#create
Similar to :only
, we have :except
to have all routes except the actions defined which can be given as,
config/routes.rbRails.application.routes.draw do
resources :homes, except: [:get, :post]
end
Then the routes available will be as below,
terminal=>$ rake routesPrefix Verb URI Pattern Controller#Actionhome GET /homes/:id(.:format) homes#show
PATCH /homes/:id(.:format) homes#update
PUT /homes/:id(.:format) homes#update
DELETE /homes/:id(.:format) homes#destroy
new_home GET /homes/new(.:format) homes#new
edit_home GET /homes/edit(.:format) homes#edit
The resulting route does not have ‘index
’ and ‘create
’ option as it is excluded using :except
.
Match route to have multiple actions using :match
Most of the time a specific route method needs more than one action. At those time option like :match
comes in role as below,
config/routes.rbRails.application.routes.draw do
resources :homes
match ‘homes/:id/reviews’ => ‘homes#reviews’, as: :reviews, via: [:get, :post]
end
It gives out the routes as below,
terminal=>$ rake routesPrefix Verb URI Pattern Controller#Actionreviews GET|POST /homes/:id/reviews(.:format) homes#reviews
Renaming routes URL using :path
Another case, when a URL (URI Pattern) needs to get changed for any particular route due to any reason, well it can be done by using :path
option as below,
config/routes.rbRails.application.routes.draw do
resources :homes, path: ‘/home-track/home’
end
Then the available routes will become like as follows,
terminal=>$ rake routesPrefix Verb URI Pattern Controller#Action homes GET /home-track/home(.:format) homes#show
PATCH /home-track/home(.:format) homes#update
PUT /home-track/home(.:format) homes#update
DELETE /home-track/home(.:format) homes#destroy
POST /home-track/home(.:format) homes#create
new_home GET /home-track/home/new(.:format) homes#new
edit_home GET /home-track/home/edit(.:format) homes#edit
Redirection of URL using :redirect
Consider any random Rails project in which there are pages for various posts (/posts/:id
) and there are some Google ranking associated with these pages, but due to some reasons the route content needs to get shifted/updated to articles(/articles/:id
). In such cases, the option to keep google ranking same as well as shifting from posts to articles (keeping the page content of articles) can be done by using :redirect
. As the name suggests :redirect
, it redirects any specific route path to the desired route path as below,
config/routes.rbRails.application.routes.draw doresources :homesget ‘homes/:id/comments => ‘homes#commentsget ‘homes/:id/reviews => ‘homes#reviews’, to: redirect(‘homes/%{id}/comments’) # for Rails version>=4end
So now whenever the URL as ‘homes/:id/reviews
’ gets a hit, it will redirect to ‘homes/:id/comments
’ and content from ‘comments
’ page will show up.