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 ch开发者_StackOverflowanges 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!
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
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
}
In Rails 4 (4.2.0 in my case), I added this: match "*path" => "main#index", :via => [:get, :post]
to app/config/routes.rb
.
To find out what you are supposed to put in place of main
. Look in this file: app/controllers/main_controller.rb
. Again, yours may not be called main_controller.rb
, but it will be something_controller.rb
, probably NOT application_controller.rb
or servers_controller.rb
. In that file, you'll see some code that looks like this:
class MainController < ApplicationController
def index
render :layout => "angular"
end
end
Where there is MainController, you should be able to tell by the error message that rails provides in your browser what to do from here. Just replace main in match "*path" => "main#index", :via => [:get, :post]
with whatever the prefix of the controller is.
Hope this makes sense. I am a ruby beginner as well
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.
精彩评论