Redirect root to named route in rails 4

I am trying to redirect the root of www.example-app.com to www.example-app.com/app/dashboard using routes.rb . At the moment, I am doing it like this:

 root to: redirect('/app/dashboard') 

But I would like to do this using a named route, for example:

 get 'app/dashboard' => 'accounts#dashboard', as: :account_dashboard 

But when I put this in the routes:

 root to: redirect(account_dashboard_url) 

... of course it does not work, how can I do this?

+5
source share
2 answers

You cannot do this directly in routes.rb (Rails 4.2 at the moment), but there are ways to make it work. The simplest, IMO, would be to create a method in your application controller for redirection.

 # routes.rb root to: 'application#redirect_to_account_dashboard' 

and

 # application_controller.rb def redirect_to_account_dashboard redirect_to account_dashboard_url end 
+3
source

Note that with Rails 5 , redirection from routes is possible.

 match "*", to: redirect('/something-else'), via: :all 

http://guides.rubyonrails.org/routing.html#redirection

This page is high enough for some Google keywords, and this can be misleading.

0
source

Source: https://habr.com/ru/post/1214384/


All Articles