从 rails 更改 URL 时遇到问题

Having trouble changing URL from rails

现在,当我点击一个国家时,我得到 country/(country_id) 示例 ...country/1

然而,当我点击一个状态时,我得到

country/1?city=5958

我想看country/1/city/1

尝试在路由文件中像下面这样嵌套路由

resources :countries do
  resources :cities
end

您的 routes.rb 中需要一些嵌套路由...并且为了约定优于配置,您应该使用复数形式。

resources :countries do
  resources :cities
end

然后当您 "click on a state" 进行展示时,link 应该类似于...

link_to country_city_path(my_country, my_city)

...当然,替换你自己的变量名。

这将创建 countries/1/cities/1 的 url,它将调用 CitiesController

中的 show 操作