How to Split Routes.Rb into Smaller Files

rails 4: split routes.rb into multiple smaller files

This was removed from Rails 4 in June of 2012. 5e7d6bba reverts an earlier commit, removing support for loading multiple external route files as part of config.rb.

For further read, check out the comments on this commit.

How to split routes.rb into smaller files

You can do that:

routes.rb

require 'application_routes'
require 'rest_api_routes'

lib/application_routes.rb

YourApplication::Application.routes.draw do
# Application related routes
end

lib/rest_api_routes.rb

YourApplication::Application.routes.draw do
# REST API related routes
end

UPDATE: (This method has since been removed from Rails)

Rails edge just got a great addition, multiple route files:

# config/routes.rb
draw :admin

# config/routes/admin.rb
namespace :admin do
resources :posts
end

This will come handy for breaking down complex route files in large apps.

Divide large routes.rb to multiple files in Rails 5

Rails 6.1+ built-in way to load routes from multiple files.

From official Rails docs:



Breaking up very large route file into multiple small ones:

If you work in a large application with thousands of routes, a single config/routes.rb file can become cumbersome and hard to read.

Rails offers a way to break a gigantic single routes.rb file into multiple small ones using the draw macro.

# config/routes.rb

Rails.application.routes.draw do
get 'foo', to: 'foo#bar'

draw(:admin) # Will load another route file located in `config/routes/admin.rb`
end

# config/routes/admin.rb

namespace :admin do
resources :comments
end

Calling draw(:admin) inside the Rails.application.routes.draw block itself will try to load a route file that has the same name as the argument given (admin.rb in this case). The file needs to be located inside the config/routes directory or any sub-directory (i.e. config/routes/admin.rb or config/routes/external/admin.rb).

You can use the normal routing DSL inside the admin.rb routing file, however you shouldn't surround it with the Rails.application.routes.draw block like you did in the main config/routes.rb file.


Link to the corresponding PR.

Refactoring a large routes.rb file in rails 4

In one of my larger applications I use the following segment of code inside of my config/routes.rb file.

class ActionDispatch::Routing::Mapper
def draw(routes_name)
instance_eval(File.read(Rails.root.join("config/routes/#{routes_name}.rb")))
end
end

YourApplication::Application.routes.draw do
# Loads config/routes/common.rb
draw :common
# Loads config/routes/another.rb
draw :another
end

Rails 4 initially had support for draw :routeName but it was removed as it did not show any improvements. (I dont know ^.^) You can check out the git commit doing so here: https://github.com/rails/rails/commit/5e7d6bba79393de0279917f93b82f3b7b176f4b5

Is it possible to include external routes files into the main routes.rb file?

(I've updated this answer to take advantage of the RouteReloader for development work)

You can easily accomplish this (even in Rails 4!).

config/routes.rb:

Rails.application.routes.draw do
resources :foo
end

config/routes/included.rb:

Rails.application.routes.draw do
resources :bar
end

config/initializers/routes.rb

Rails.application.routes_reloader.paths.unshift *Dir[File.expand_path("../../routes/**/*.rb", __FILE__)]

This will add all files under config/routes to the application routes, and it'll probably add them in reverse lexical order by filename. If you want to load the routes in a different order, rather than the glob, you can just push or unshift the routes onto routes_reloader.paths in the order desired.

rake routes:

   Prefix Verb   URI Pattern             Controller#Action
foo_index GET /foo(.:format) foo#index
POST /foo(.:format) foo#create
new_foo GET /foo/new(.:format) foo#new
edit_foo GET /foo/:id/edit(.:format) foo#edit
foo GET /foo/:id(.:format) foo#show
PATCH /foo/:id(.:format) foo#update
PUT /foo/:id(.:format) foo#update
DELETE /foo/:id(.:format) foo#destroy
bar_index GET /bar(.:format) bar#index
POST /bar(.:format) bar#create
new_bar GET /bar/new(.:format) bar#new
edit_bar GET /bar/:id/edit(.:format) bar#edit
bar GET /bar/:id(.:format) bar#show
PATCH /bar/:id(.:format) bar#update
PUT /bar/:id(.:format) bar#update
DELETE /bar/:id(.:format) bar#destroy

how to reload routes /config/routes/* in rails 4?

You can use:

Rails.application.reload_routes!

You can read about it here (will have to use find)

Rails routing - a good way to split the edit form into separate parts?

post/23/gallery/edit looks more semantic. gallery - resource related to post and edit - action on this resource. actual modification will have post/23/gallery url with POST, PUT, DELETE methods, which is more REST



Related Topics



Leave a reply



Submit