如何覆盖 Rails 中的 `root_path`?

How to override `root_path` in Rails?

我的语言环境定义如下:

# routes.rb
scope "(:locale)", locale: /#{I18n.available_locales.join("|")}/ do
  ...
  root "home#index"
end

我还在 ApplicationController 中设置了语言环境 default_url_params
这样我的所有路线和网址都包含这样的语言环境:http://example.com/en/something
http://example.com/fr/something
这对我来说是预期的行为。

我也有这样的根网址:
http://example.com/en
http://example.com/fr

我想要实现的是 exclude en root_path(默认)语言环境,以便 root_path 适合 :en 语言环境(显式传递或通过 default_url_params)是 http://example.com/

有什么办法吗? 我可以覆盖 root_path 以在其中进行黑客攻击吗?

谢谢。

重写 ApplicationController 中的 root_path 方法:

def root_path
  (I18n.locale == 'en') ? '/' : super
end