plunntic iam
plunntic iam

Reputation: 994

Redirect root to named route in rails 4

I'm trying to redirect my www.example-app.com root to www.example-app.com/app/dashboard using routes.rb. At the moment i'm doing it like so:

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

But would like to do that using named route, for example:

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

But when i put that in routes:

root to: redirect(account_dashboard_url)

... of course it doesn't work, how can i do it?

Upvotes: 8

Views: 7387

Answers (2)

Frederik Spang
Frederik Spang

Reputation: 3454

Please note, that as of Rails 5, redirecting from the routes is possible.

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

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

This page is quite high on some Google-keywords, and this might be misleading.

Upvotes: 1

dbenton
dbenton

Reputation: 313

You can't do this directly in routes.rb (as of now – Rails 4.2), but there are ways to make it work. The simplest, IMO, would be to create a method in your application controller to do the 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

Upvotes: 6

Related Questions