Map a Resourceful Route in Rails to Another Controller Class
July 29, 2024
You can map a resource to a different controller by passing the controller option to the resources method.
This post is part of my handbook on the Rails Router.
You can map a resource to a different controller by passing the controller option to the resources method.
resources :posts, controller: "articles"
Without the controller option, the posts resource route will map to the PostsController class.
With the controller option provided, it will map to the ArticlesController class.
$ bin/rails routes -g posts
Prefix Verb URI Pattern Controller#Action
posts GET /posts(.:format) articles#index
POST /posts(.:format) articles#create
new_post GET /posts/new(.:format) articles#new
edit_post GET /posts/:id/edit(.:format) articles#edit
post GET /posts/:id(.:format) articles#show
PATCH /posts/:id(.:format) articles#update
PUT /posts/:id(.:format) articles#update
DELETE /posts/:id(.:format) articles#destroy
Sign up for my newsletter
Let's learn to become better developers.