Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Refactoring a large routes.rb file in rails 4

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?

like image 442
Cyrus Avatar asked Nov 05 '13 02:11

Cyrus


3 Answers

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

like image 127
Michael Lynch Avatar answered Nov 10 '22 15:11

Michael Lynch


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

Looks like this ability was deprecated in Rails 4.

like image 38
CDub Avatar answered Nov 10 '22 13:11

CDub


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.

like image 1
Dzung Nguyen Avatar answered Nov 10 '22 13:11

Dzung Nguyen