16

I'm working w/ a Rails 3 application and I want to split up the routes into separate files depending on the subdomain. Right now I have this in my routes.rb file:

Skateparks::Application.routes.draw do
  constraints(:subdomain => 'api') do
    load 'routes/api.rb'
  end
end

And In my routes/api.rb file I have:

resources :skateparks

This doesn't seem to work though because if I run rake routes I get

undefined method `resources' for main:Object

Also, if I try to navigate to http://0.0.0.0:3000/ I get:

Routing Error

No route matches "/"

4 Answers 4

7

In Rails 3.2, config.paths is now a hash, so @sunkencity's solution can be modified to:

# config/application.rb
config.paths["config/routes"] << File.join(Rails.root, "config/routes/fooroutes.rb")
2
  • 1
    Thanks! We have multiple files in that dir; this worked fine: config.paths["config/routes"] += Dir[Rails.root.join("config/routes/*.rb")]
    – Henrik N
    Mar 12, 2013 at 7:46
  • 1
    And if you tack on a Dir[…].sort I believe you can rely on that ordering.
    – Henrik N
    Mar 12, 2013 at 9:21
4

Sunkencity's answer seems to be identical to the following link, but for completeness' sake: https://rails-bestpractices.com/posts/2011/05/04/split-route-namespaces-into-different-files/

Note that routes defined later will override routes defined earlier. However, if you use something like

    config.paths.config.routes.concat(
        Dir[Rails.root.join('config/routes/*.rb')])

you don't know in what order the files will be read. So use

    config.paths.config.routes.concat(
        Dir[Rails.root.join('config/routes/*.rb')].sort)

instead, so you at least know they will be in alphabetical order.

0

Add the route file to the app route loading path:

# config/application.rb
config.paths.config.routes << File.join(Rails.root, "config/routes/fooroutes.rb")

Wrap your other route file in a block like this.

#config/routes/fooroutes.rb
Rails.application.routes.draw do |map|
  match 'FOO' => 'foo/bar'
end

Works for me in rails 3.0

4
  • Removed the suggestion for engines, and added config routes. should work!
    – sunkencity
    Sep 6, 2011 at 8:22
  • 2
    I get the error undefined method config' for #<Rails::Paths::Root:0x007fc3fdd1e7a0` using the config.paths.config.routes
    – Nate Bird
    Feb 6, 2012 at 15:20
  • i too got the same. any body knows how to fix this ? Dec 21, 2012 at 12:43
  • Doesn't work with Rails 3.2, but see Sam Gibson's answer.
    – Henrik N
    Mar 12, 2013 at 7:48
0

We used this in our app:

    config.paths['config/routes'] = Dir["config/routes/*.rb"]

If you try to access config.paths['config/routes'] normally, it returns the relative path to config/routes.rb, so by doing the above you're giving it relative paths to all of the files in your routes folder and removing the reference to config/routes.rb

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.