Passion make things more better

Rails, Swiftあたりの話題を中心に書いています

RailsでRoutesを分割する

config/routes.rb

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

Rails.application.routes.draw do

  root to: 'pages#home'

  draw :common
  draw :admin

end

config/routes/xxx.rb

Rails.application.routes.draw do
  namespace :admin do
    root to: 'pages#home'
  end
end

これでapp/config/routes以下にファイルを配置していけばOK。