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

前端之家收集整理的这篇文章主要介绍了将root重定向到rails 4中的命名路由前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试使用routes.rb将我的www.example-app.com根目录重定向到www.example-app.com/app/dashboard.目前我正是这样做的:
root to: redirect('/app/dashboard')

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

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

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

root to: redirect(account_dashboard_url)

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

解决方法

你不能直接在routes.rb中做到这一点(截至目前为止–Rails 4.2),但有一些方法可以使它工作.最简单的IMO是在应用程序控制器中创建一个方法来进行重定向.
# routes.rb

root to: 'application#redirect_to_account_dashboard'

# application_controller.rb

def redirect_to_account_dashboard
  redirect_to account_dashboard_url
end

猜你在找的Linux相关文章