Redirect root url to somewhere else in Rails application

43,412

Solution 1

You will need to set the controller to a Welcome or what not, then when that controller is hit, it will redirect to the route you want. Maybe Rails 3 routing will be better for stuff like this, but for right now, you will need to have a main root controller.

Solution 2

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

Solution 3

redirect options don't seem to be documented too well.
here you go (@derek, see last example):

redirect to a subdomain on the current request's domain

root to: redirect(subdomain: 'foo', path: '/bar') # => foo.example.com/bar

redirect with substituted params from the matched route

get 'stores/:name(*all)', to: redirect(subdomain: 'stores', path: '/%{name}%{all}')

redirect with status code (eg. 302 instead of default 301)

redirect(path: '/foo', status: 302)

redirect with a conditional block

redirect(status: 302) { |params, request|
  case request.host
  when 'localhost'
    '/foo'
  when /example.com$/
    '/bar'
  else
    '/baz'
  end
}
Share:
43,412
klew
Author by

klew

Updated on July 09, 2022

Comments

  • klew
    klew almost 2 years

    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!

  • klew
    klew over 14 years
    But how add this :locale parameter to redirect_to prepayments_root_path ?
  • klew
    klew over 14 years
    I know I can add it with some string before, but is there a nicer way?
  • Sachintha Udara
    Sachintha Udara over 14 years
    Can you give output of your rake routes?
  • Derek Dahmer
    Derek Dahmer almost 11 years
    This uses a 301 permanent redirect. How would you do a 302 temporary redirect?
  • Tom Rossi
    Tom Rossi over 10 years
    I'm with @DerekDahmer! How do you do a 302 from the routes.rb. I tried passing in status: 302, but no joy.
  • Lloeki
    Lloeki over 10 years
    Obsolete answer. @glasz's one is the most complete and up to date.
  • Shaun McDonald
    Shaun McDonald over 10 years
    Can you see if this answer does the trick? stackoverflow.com/a/19138779/99266
  • Richard Peck
    Richard Peck over 7 years
    guides.rubyonrails.org/routing.html#redirection To do a temp (302) redirect, just add status: 302 to the redirect directive: get '/stories/:name', to: redirect('/articles/%{name}', status: 302)
  • Edward Anderson
    Edward Anderson over 2 years
    Still works as of Rails 6