gpt4 book ai didi

ruby-on-rails - 如何在 Rails 中覆盖 `root_path`?

转载 作者:数据小太阳 更新时间:2023-10-29 07:55:25 26 4
gpt4 key购买 nike

我有这样定义的语言环境:

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

我还在 ApplicationControllerdefault_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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com