我正在尝试为我的路线做一个简单的语言翻译,但我真的不明白为什么我的方法不起作用。我想www.example.com/it/camere
打电话rooms_path(locale: :it)
和www.example.com/en/rooms
打电话rooms_path(locale: :en)
。
例如,查看以下路线:
get ":locale/rooms" => "home#rooms", constraints: { locale: "en" }, as: "rooms"
get ":locale/camere" => "home#rooms", as: "rooms"
我得到以下结果:
app.rooms_path # => error OK
app.rooms_path(locale: "it") # => 'it/camere' OK
app.rooms_path(locale: "en") # => 'en/camere' Why?!?
并使用以下路线(注意第二个约束):
get ":locale/rooms" => "home#rooms", constraints: { locale: "en" }, as: "rooms"
get ":locale/camere" => "home#rooms", constraints: { locale: "it" }, as: "rooms"
我得到以下结果:
app.rooms_path # => error OK
app.rooms_path(locale: "it") # => 'it/camere' OK
app.rooms_path(locale: "en") # => error Lol?!? This is weird!
有人可以向我解释为什么会这样吗?
我已经阅读了大约 3 次关于路由的 Rails 文章,但它没有帮助。
PS我宁愿不使用插件来学习:)
编辑:
这是我通过调用 rake 路由得到的:
rooms GET /:locale/rooms(.:format) home#rooms {:locale=>"en"}
rooms GET /:locale/camere(.:format) home#rooms {:locale=>"it"}
root /(:locale)(.:format) home#index