将root重定向到rails 4中的命名路由

plu*_*iam 7 redirect routes ruby-on-rails-4

我正在尝试将我的www.example-app.comroot 重定向到www.example-app.com/app/dashboard使用routes.rb.目前我正是这样做的:

root to: redirect('/app/dashboard')
Run Code Online (Sandbox Code Playgroud)

但是想使用命名路由来做,例如:

get 'app/dashboard' => 'accounts#dashboard', as: :account_dashboard
Run Code Online (Sandbox Code Playgroud)

但是当我把它放在路线中时:

root to: redirect(account_dashboard_url)
Run Code Online (Sandbox Code Playgroud)

......当然它不起作用,我怎么能这样做?

dbe*_*ton 5

您不能直接在routes.rb中进行此操作(到目前为止,是Rails 4.2),但是有一些方法可以使其工作。最简单的IMO是在应用程序控制器中创建一个方法来进行重定向。

# routes.rb

root to: 'application#redirect_to_account_dashboard'
Run Code Online (Sandbox Code Playgroud)

# application_controller.rb

def redirect_to_account_dashboard
  redirect_to account_dashboard_url
end
Run Code Online (Sandbox Code Playgroud)