Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
240 views
in Technique[技术] by (71.8m points)

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!

question from:https://stackoverflow.com/questions/2066504/redirect-root-url-to-somewhere-else-in-rails-application

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...