Why rails app is redirecting unexpectedly instead of matching the route?

rails-routing, ruby-on-rails

Solution

We meet again, ruevaughn. :)

I created a test rails app and the following minimal example works for me:

scope ":locale", locale: /#{I18n.available_locales.join("|")}/ do
  resources :sites do
    collection do
      get :admin
    end
  end

  root to: "locale#root" # handles /en/
  match "*path", to: "locale#not_found" # handles /en/fake/path/whatever
end

root to: redirect("/#{I18n.default_locale}") # handles /
match '*path', to: redirect("/#{I18n.default_locale}/%{path}") # handles /not-a-locale/anything

Problem

I asked this question earlier and thought it was fixed, but it's not. Previous question here My problem is I am trying to set my routes so that when I type in localhost:3000/sites/admin It should redirect to localhost:3000/en/sites/admin here is my routes.rb file ``` scope ":locale", locale: /#{I18n.available_locales.join("|")}/ do get "log_out" => "sessions#destroy", as: "log_out" get "log_in" => "sessions#new", as: "log_in" resources :sites, except: [:new, :edit, :index, :show, :update, :destroy, :create] do collection do get :home get :about_us get :faq get :discounts get :services get :contact_us get :admin get :posts end end resources :users resources :abouts resources :sessions resources :coupons resources :monthly_posts resources :reviews resources :categories do collection { post :sort } resources :children, :controller => :categories, :only => [:index, :new, :create, :new_subcategory] end resources :products do member do put :move_up put :move_down end end resources :faqs do collection { post :sort } end root :to => 'sites#home' match "/savesort" => 'sites#savesort' end match '', to: redirect("/#{I18n.default_locale}") match '*path', to: redirect("/#{I18n.default_locale}/%{path}") ``` But as of right now, it redirects to /en/en/en/en/en/en/en/en/en/en/sites/admin (adds en until browser complains). Any thoughts why it keeps adding /en? Edit: The answer is great, thanks. Can you help me diagnose the root route? ``` root to: redirect("#{/#{I18n.default_locale}") # handles / ``` I know redirects is looking for something like ``` redirect("www.example.com") ``` So that leaves this part ``` #{/#{I18n.default_locale} ``` The #{ is using rubys string interpolation, right? i'm not sure what that { is doing though. So then we have ``` /#{I18n.default_locale} ``` Which is also using string interpolation and to print out the value of I18n.default_locale? Hopefully that makes sense, I really really appreciate the help, I am learning a lot. Edit 2: I changed the line from ``` root to: redirect("#{/#{I18n.default_locale}") # handles / ``` to ``` root to: redirect("/#{I18n.default_locale}") # handles / ``` But i'm not sure if thats right. Now i'm getting the error ``` uninitialized constant LocaleController ``` I know it's getting the error from the root to: "locale#root", but i thought the locale# would come from the scope. I'll continue playing with it and let you know any progress. Here is a new link to my routes file https://gist.github.com/2332198

Original source