Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirect root url to somewhere else in Rails application

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!

like image 985
klew Avatar asked Jan 14 '10 18:01

klew


2 Answers

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

like image 102
Shaun McDonald Avatar answered Oct 14 '22 09:10

Shaun McDonald


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
}
like image 38
glasz Avatar answered Oct 14 '22 11:10

glasz