Why is my rails app redirecting unexpectedly instead of mapping a route?
I've asked this question before and thought it was fixed, but it isn't. Previous question here
My problem is that I am trying to set routes so that when I type
local: 3000 / sites / admin
It should redirect to
local: 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 now, it redirects to / en / 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 are looking for something like
redirect("www.example.com")
So this part
#{/#{I18n.default_locale}
# {uses rubys string interpolation, right? I'm not sure what this (does).
So we have
/#{I18n.default_locale}
Which also uses string interpolation and prints out the value of I18n.default_locale?
Hope this 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 this is correct. Now I am getting the error
uninitialized constant LocaleController
I know it got an error from the root: "locale # root", but I thought the locale # would be from scope.
I will continue to play with him and let you know any progress.
Here is a new link to my routes file https://gist.github.com/2332198
source to share
We meet again, ruevaughn. :)
I created a test rail 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
source to share
With Rails 4.0.x, what's %{path}
in the redirection will come out of the slashes in the path, so you end up with an infinite loop redirecting to/en/en%2Fen%2Fen%2Fen...
Just in case, someone like me is looking for a suitable Rails-4 solution, here is what I found to work without problems, even with more complex paths to be redirected:
# Redirect root to /:locale if needed
root to: redirect("/#{I18n.locale}", status: 301)
# Match missing locale paths to /:locale/path
# handling additional formats and/or get params
match '*path', to: (redirect(status: 307) do |params,request|
sub_params = request.params.except :path
if sub_params.size > 0
format = sub_params[:format]
sub_params.except! :format
if format.present?
"/#{I18n.locale}/#{params[:path]}.#{format}?#{sub_params.to_query}"
else
"/#{I18n.locale}/#{params[:path]}?#{sub_params.to_query}"
end
else
"/#{I18n.locale}/#{params[:path]}"
end
end), via: :all
# Redirect to custom root if all previous matches fail
match '', to: redirect("/#{I18n.locale}", status: 301), via: :all
source to share