How to split routes.rb into smaller files

后端 未结 3 1435
醉梦人生
醉梦人生 2020-12-17 17:39

Is it possible to split Rails 3.X routes.rb file?

We have so many resources it is difficult to find them. I would like to split at least APP and REST API routes.

3条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-17 18:34

    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.

提交回复
热议问题