Map a Resourceful Route in Rails to Another Controller Class

Map a Resourceful Route in Rails to Another Controller Class

1 min read
💡
This post is part of my handbook on the Rails Router.
The Rails Router
Everything you need to know about the incredible routing system in Rails.

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