Refactoring a large routes.rb file in rails 4

2019-02-02 16:31发布

I'm in the process of upgrading a rails 3 app to rails 4.0.1.

In my rails 3 app I have the following code in the my application.rb to use multiple route files.

config.paths["config/routes"] += Dir[Rails.root.join('config', 'routes', '*.rb').to_s]

but that throws an exception when I try to use the same thing in rails 4.

Any tips?

3条回答
ゆ 、 Hurt°
2楼-- · 2019-02-02 17:06

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

查看更多
Rolldiameter
3楼-- · 2019-02-02 17:11

Check out this SO answer: rails 4: split routes.rb into multiple smaller files

Looks like this ability was deprecated in Rails 4.

查看更多
趁早两清
4楼-- · 2019-02-02 17:13

I don't know how big your application is. But you should look into routing concern in rails 4, if you need some proper refactoring with Rails route.

Mo' files, mo' problems.

查看更多
登录 后发表回答