Redirect root url to somewhere else in Rails application

klew picture klew · Jan 14, 2010 · Viewed 40k times · Source

I have routes like this:

map.namespace 'prepayments', :path_prefix => '/:locale/prepayments'  do |prepayment|
  prepayment.root  :controller => 'login', :namespace => 'prepayments'
  ...
end

map.redirect '/', :controller => 'prepayments/login' # this is not working
# I tried also
map.root :controller => 'prepayments/login'

What I would like to get is that after typing: www.example.com it would redirect me to www.example.com/en/prepayments.

Earlier when I used map.root from above example it just stayed at www.example.com and rendered correct view (but it was without :locale and it worked good), later I added :locale to my routes and from this time my view (that uses some form) doesn't work properly. I get error that it can't find corresponding route for form - which is right, because I didn't pass any :locale.

So, how to redirect root to another page? It will probably need to generate correct path and pass it through http 302. Or/And how to make something like:

map.root :controller => 'prepayments/login', :my_locale => 'en'

EDIT: My rake routes looks like this:

         prepayments_root  /:locale/prepayments               {:controller=>"prepayments/login", :action=>"index"}
       prepayments_create  /:locale/prepayments/send_email    {:method=>:post, :controller=>"prepayments/login", :action=>"send_email"}
         prepayments_home  /:locale/prepayments/home          {:controller=>"prepayments/prepayments", :action=>"home"}
         prepayments_save  /:locale/prepayments/save          {:controller=>"prepayments/prepayments", :action=>"save"}
        prepayments_agree  /:locale/prepayments/agree         {:controller=>"prepayments/prepayments", :action=>"agree"}
     prepayments_disagree  /:locale/prepayments/disagree      {:controller=>"prepayments/login", :action=>"logout"}
      prepayments_payment  /:locale/prepayments/payment       {:controller=>"prepayments/prepayments", :action=>"payment"}
prepayments_payment_email  /:locale/prepayments/payment_email {:controller=>"prepayments/prepayments", :action=>"payment_email"}
                           /:locale/prepayments/:uid          {:controller=>"prepayments/login", :action=>"verify"}
                 redirect  /                                  {:controller=>"prepayments/login", :action=>"index"}

EDIT:

I tried doing it in the way Garrett proposed and it worked. I changed routes:

map.redirect '/', :controller => 'prepayments/login', :action => 'welcome'

and added welcome method in controller:

def welcome
  redirect_to prepayments_root_path(:locale => 'en')
end

And it works as I wanted (so it changes url in my browser).

The other way is to change routes like this:

map.root :controller => 'prepayments/login', :locale => 'en'

It also works, but it isn't redirecting (it doesn't change url in browser). I'm not sure if there is such option as map.redirect. I found it in examples on www but I also found plugin that add such functionality.

Thanks for help!

Answer

Shaun McDonald picture Shaun McDonald · Sep 2, 2011

In Rails 3 you can write:

root :to => redirect('/prepayments')

The following page has a good introduction to doing these redirects in Rails 3: http://www.railsdispatch.com/posts/rails-routing