How to split routes.rb into smaller files - ruby-on-rails

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.
Thanks!

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.

In Rails3, you can set the configs in config/application.rb
config.paths.config.routes.concat Dir[Rails.root.join("config/routes/*.rb")]

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

Related

How do I create multiple route files in Rails 4?

What's the best approach in Rails 4 to define routes from multiple files? Should I be creating them and using some kind of include mechanism from routes.rb, or is there something else I should be doing?
I'm aware of a new feature in Rails 4 called concerns, although they seem unrelated to what I'm trying to do. I don't want to modify existing routes, I just want to split my definitions up into multiple files to prevent routes.rb from getting too big.
In your application.rb file:
routes = Dir[Rails.root.join("config/routes/*.rb")] + config.paths['config/routes.rb']
config.paths['config/routes.rb'] = routes
You can define routes in any file under "config/routes" as in:
# config/routes/api_routes.rb
Rails.application.routes.draw do
namespace :api do
resources :posts
end
end
In Rails 4 it looks like the key on config.paths changed from 'config/routes' to 'config/routes.rb'
You should keep them all in one file and namespace anything you need to separate.
YourApp::Application.routes.draw do
namespace :api do
resources :your_model
end
end
Then you put the api controllers in the directory controllers/api/your_model_controller.rb
I've done this for API's before and never had a problem, but you should require some kind of validation for the api namespace

Rails 3.1 separating routes.rb into multiple files [duplicate]

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.
Thanks!
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.
In Rails3, you can set the configs in config/application.rb
config.paths.config.routes.concat Dir[Rails.root.join("config/routes/*.rb")]
Rails 3.2.11
config.paths["config/routes"].concat Dir[Rails.root.join("config/routes/*.rb")]

Translating routes in Rails 3.1 without any gem

In a previous Rails 2.3 project I used the translate_routes gem to perform the translation of the routes. It worked great.
In my new Rails 3.1 project, again, I need route translation. Unfortunately, translate_routes doesn't work any longer and Raul its developer announced that he would no longer maintain the gem.
I tried to work with one of the project's fork that is supposed to be ok on Rails 3.1, but I couldn't do much of it.
Is there a way to build route translations without a gem ?
Here an example of a working route without translation.
constraints(:subdomain => 'admin') do
scope "(:locale)", :locale => /fr|de/ do
resources :country, :languages
match '/' => 'home#admin', :as => :admin_home
end
end
As you can see, I also want to have a default route without locale that is used for my default locale : en.
Has anyone done that before?
Thanks
Probably a little bit late for you, but it may be helpful for others, try a fork of translate_routes:
https://github.com/francesc/rails-translate-routes
Saw your post earlier, but found out another sollution later.
I wanted to translate Rails routes and their default resource actions, but I didn't like they way rails-translate-routes added _nl to my default path-names.
I ended up doing this (also works in rails 4.0), which should be a good sollution when you are presenting your app in only 1 or 2 languages.
# config/routes.rb
Testapp::Application.routes.draw do
# This scope changes resources methods names
scope(path_names: { new: I18n.t('routename.new'), edit: I18n.t('routename.edit') }) do
# devise works fine with this technique
devise_for :users, path: I18n.t('routename.userspath')
# resource path names can be translated like this
resources :cars, path: I18n.t('routename.carspath')
# url prefixes can be translated to
get "#{I18n.t('routename.carspath')}/export", to: 'cars#export'
end
end
And
# config/locales/nl.yml
nl:
routename:
## methods
new: 'nieuw'
edit: 'aanpassen'
## resources, etc.
userpath: 'gebruikers'
carspath: 'voertuigen'
Result in:
/voertuigen
/voertuigen/nieuw
/voertuigen/aanpassen
/voertuigen/export
update and destroy are not neccesairy since they link into the root as post actions. Save your work ;)

configurable routes

I'm working on a small rails gem where my config/routes.rb looks like this.
resources :blog_posts do
resources :blog_comments
resources :blog_images
collection do
get :drafts
end
end
I guess it works as i should, but I would like :blog_posts to be something configurable. Thus not forcing the gem to be hard coded into using the url /blog_posts/
What is the best way to do this?
You should look into "mounting" of routes so that users of your gem can mount your engine's routes at a point of their choosing. I suggest having a look at device gem.

How many routes in Rails before performance is affected? What key changes did you make to a scaffold/default generated Routes file?

We are on Rails 2.3.
What is the upper bound on the number of routes Routes.rb can contain before performance is affected?
What are the key changes you made to the default Routes file created by scaffolding?
We like the default scaffolding in development since we can easily access/modify objects from the web as opposed to the database. However, this configuration is not tenable for a production environment. Is there a way to "activate" certain routes while in development?
One thing I always do is if you are doing resources, I always limit the routes by doing something like:
map.resources :comments, :only => [:create, :destroy, :index]
You can just put the following in your routes.rb file
if ENV['RAILS_ENV'] == 'production'
#production routes
else
#development routes
end

Resources