Working with multiple root paths and areas in Rails

We have the following route setup:

MyApp::Application.routes.draw do
  scope "/:locale" do    
    ...other routes
    root :to => 'home#index'
  end
  root :to => 'application#detect_language'
end

What gives us this:

root      /:locale(.:format)    home#index
root      /                     application#detect_language

what well.

However, when we want to create a route from locales, we run into a problem:

root_pathgenerates /which is correct.

root_path(:locale => :en)generates /?locale=enwhat is undesirable - we want/en

So the question is, is this possible and how?

+5
source share
1 answer

The root method is used by default to determine the top level / route. Thus, you define the same route twice, as a result of which the second definition redefines the first!

Here is the root method definition:

def root(options = {})
  options = { :to => options } if options.is_a?(String)
  match '/', { :as => :root, :via => :get }.merge(options)
end

, : root . , . .

scope "/:locale" do    
  ...other routes
  root :to => 'home#index', :as => :root_with_locale
end
root :to => 'application#detect_language'

:

root_with_locale_path(:locale => :en)

, !

+7

All Articles