我有这样定义的语言环境:
# 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
(default) locale for root_path
,这样 root_path
:en
语言环境(显式传递或通过 default_url_params
)是 http://example.com/
有什么办法吗?
我可以覆盖 root_path
以在其中进行黑客攻击吗?
谢谢。
最佳答案
覆盖 ApplicationController
中的 root_path
方法:
def root_path
(I18n.locale == 'en') ? '/' : super
end
关于ruby-on-rails - 如何在 Rails 中覆盖 `root_path`?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32464663/