How do I create multiple route files in Rails 4? - ruby-on-rails

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

Related

Multiple paths for one Rails resource

Is it possible to send multiple paths to the same resource in Rails?
Example: route both '/foo-bars' and '/foo_bars' to resource :foo_bars
Maybe you'd prefer a permanent redirect instead? Browsers will cache it and possibly less maintenance problems you'll have later on (1 path = 1 resource is something rails programmers typically take for granted)
http://guides.rubyonrails.org/routing.html#redirection
get '/stories/:name', to: redirect('/articles/%{name}')
This may work:
resources :foo_bars
resources :foo_bars, path: "foo-bars", as: "foo-bars"
The as will also alias your path/url helpers, omitting it requires you to use one set of helpers (the originally defined ones).
resources :foo_bars, :foo-bars, controller: :foo_bars do
# nested
end
This (not tested) should give you multiple sets of routes pointing to the same controller.

How to override spree users api

I have integrated spree with my application. I have overridden spree-user model to user model. I am trying to override api/users to come to my UsersController but instead everytime when I make a call to /api/users/, the control goes to Spree::Api::UsersController. I have the following route for user
namespace :api do
resources :users
end
But still my routes are overridden. What changes should I do to route /api/users/ to my local controller?
Try using Spree::Core::Engine.routes.prepend in routes.rb:
Spree::Core::Engine.routes.prepend do
namespace :api do
resources :users
end
end
Alternatively, try appending main_app when you call the route in your view code, eg:
main_app.api_users_path
Not sure if you have tried that already but hopefully it helps. You can read more at the SO threads below:
Adding Routes to Rails' Spree E-Commerce
How to add new view to Ruby on Rails Spree commerce app?
access rails url helper from deface

Rails generate controller (under a namespace)

I want to add a controller and its routes entry under a namespace (api) for which I am proceeding with rails generate api/Users my_method, which creates the files and entries as follows:
create app/controllers/api/users_controller.rb
route get "users/my_method"
invoke erb
create app/views/api/users
create app/views/api/users/my_method.html.erb
Everything worked fine apart from the routes entry. What I am assuming is it should create the routes entry as well under the correct namespace or it shouldn't create it at all, or I am doing something wrong.On the other hand when going with scaffold way it does correctly.
Is it something which we need to do it manually?
Using ruby 2.0 and rails 4 for the application.
Type in terminal
rails generate scaffold Api::User username email
rake db:migrate
this is part of result
class Admin::ServicesController < ApplicationController
# GET /api/users
# GET /api/users.json
def index
#api_users = Api::User.all
end
To do generate and I think you will understand everything, don't forget see new app structure :-), Good luck, and solve your problem quickly as possible.
You can namespace your routes in your config/routes.rb like this
namespace :api do
resources :user
end

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")]

How to split routes.rb into smaller files

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")]

Resources