将 root 重定向到 rails 中的命名路由 4
Redirect root to named route in 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),但有一些方法可以让它工作。在我看来,最简单的方法是在您的应用程序控制器中创建一个方法来进行重定向。
# routes.rb
root to: 'application#redirect_to_account_dashboard'
和
# application_controller.rb
def redirect_to_account_dashboard
redirect_to account_dashboard_url
end
请注意,从 Rails 5 开始,可以从路由重定向。
match "*", to: redirect('/something-else'), via: :all
http://guides.rubyonrails.org/routing.html#redirection
此页面在某些 Google 关键字上排名很高,这可能会产生误导。
我正在尝试使用 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),但有一些方法可以让它工作。在我看来,最简单的方法是在您的应用程序控制器中创建一个方法来进行重定向。
# routes.rb
root to: 'application#redirect_to_account_dashboard'
和
# application_controller.rb
def redirect_to_account_dashboard
redirect_to account_dashboard_url
end
请注意,从 Rails 5 开始,可以从路由重定向。
match "*", to: redirect('/something-else'), via: :all
http://guides.rubyonrails.org/routing.html#redirection
此页面在某些 Google 关键字上排名很高,这可能会产生误导。