6

I have a Rails 4 app where all controllers and views are divided into two namespaces, an agent-facing backend and a customer-facing frontend:

MyApp::Application.routes.draw do
  constraints subdomain: "admin" do
    namespace :backend do
      resources :events
    end
  end

  constraints subdomain: /.+/ do                
    namespace :frontend do
      resources :events
    end
  end
end

Additionally, all controllers inherit from either BackendController or FrontendController:

controllers
├── application_controller.rb
├── backend
│   ├── events_controller.rb
├── backend_controller.rb
├── frontend
│   └── events_controller.rb
├── frontend_controller.rb

Right now everything that needs to generate a path has to be prefixed with the namespace, such as respond_with [:backend, @event] or form_for [:backend, @event].

Is there a way to set a default namespace for URL helpers per-controller, so that respond_with @event called from a controller that inherits from BackendController automatically assumes the correct namespace?

janfoeh
  • 10,243
  • 2
  • 31
  • 56
  • Strictly speaking, the default namespace is the global namespace, and there is no option to change it. You might want to move the Backend namespace into global, or set up your own helpers to support the behavior you want. It is painful to fight the defaults though. – prusswan Jan 22 '16 at 09:49

1 Answers1

0

You can use scope module: :backend instead of namespace :backend in your routes file.

If you want the module name to appear in the URL, you can add a :path option to the scope as well.

Consider the following routes file:

Rails.application.routes.draw do

  scope module: 'backend1' do
    resources :event1
  end

  scope module: 'backend2', path: 'backend3' do
    resources :event2
  end

  namespace 'backend3' do
    resources :event3
  end

end

This will produce the following routes:

               Prefix Verb   URI Pattern                         Controller#Action
         event1_index GET    /event1(.:format)                   backend1/event1#index
                      POST   /event1(.:format)                   backend1/event1#create
           new_event1 GET    /event1/new(.:format)               backend1/event1#new
          edit_event1 GET    /event1/:id/edit(.:format)          backend1/event1#edit
               event1 GET    /event1/:id(.:format)               backend1/event1#show
                      PATCH  /event1/:id(.:format)               backend1/event1#update
                      PUT    /event1/:id(.:format)               backend1/event1#update
                      DELETE /event1/:id(.:format)               backend1/event1#destroy
         event2_index GET    /backend3/event2(.:format)          backend2/event2#index
                      POST   /backend3/event2(.:format)          backend2/event2#create
           new_event2 GET    /backend3/event2/new(.:format)      backend2/event2#new
          edit_event2 GET    /backend3/event2/:id/edit(.:format) backend2/event2#edit
               event2 GET    /backend3/event2/:id(.:format)      backend2/event2#show
                      PATCH  /backend3/event2/:id(.:format)      backend2/event2#update
                      PUT    /backend3/event2/:id(.:format)      backend2/event2#update
                      DELETE /backend3/event2/:id(.:format)      backend2/event2#destroy
backend3_event3_index GET    /backend3/event3(.:format)          backend3/event3#index
                      POST   /backend3/event3(.:format)          backend3/event3#create
  new_backend3_event3 GET    /backend3/event3/new(.:format)      backend3/event3#new
 edit_backend3_event3 GET    /backend3/event3/:id/edit(.:format) backend3/event3#edit
      backend3_event3 GET    /backend3/event3/:id(.:format)      backend3/event3#show
                      PATCH  /backend3/event3/:id(.:format)      backend3/event3#update
                      PUT    /backend3/event3/:id(.:format)      backend3/event3#update
                      DELETE /backend3/event3/:id(.:format)      backend3/event3#destroy

Hope that helps!

Raffael
  • 2,639
  • 16
  • 15
  • See also the [Rails Routing from the Outside In](guides.rubyonrails.org/routing.html#prefixing-the-named-route-helpers) Rails Guide. – Raffael Mar 28 '15 at 08:23
  • `namespace 'foo'` is just a shorthand for `scope module: 'foo', path: 'foo', as: 'foo'` – Raffael Mar 28 '15 at 08:24
  • I'm afraid you have misunderstood the question. It's not about defining routes, but about _using_ them in URL helpers. Also, you have conveniently used different resources ;) Try your route file with just three times `resources :events`, and you'll see why it does not work. – janfoeh Mar 28 '15 at 09:10