1

我有VehicleSTI 模型CarMotorcycle.

我目前的路线配置如下:

resources :vehicles
resources :cars
resources :motorcycles

但是,我认为它看起来会像这样更漂亮:

/vehicles/ #all the methods of vehicles_controller
/vehicles/cars/ #all the methods of cars_controller
/vehicles/motorcycles/ #all the methods of motorcycles_controller

我应该如何配置它以获取该 URL?

4

3 回答 3

2

我认为您可以执行以下操作:

resources :vehicles
scope "/vehicles" do
  resources :cars, :motorcycles
end

或者这样:

resources :vehicles
resources :cars, :path => "/vehicles/cars"
resources :motorcycles, :path => "/vehicles/motorcycles"

您可以在此处找到有关 Rails 路由的更多信息:http: //guides.rubyonrails.org/routing.html

于 2013-05-10T11:30:32.503 回答
1

你可以写 :

resources :vehicles do
  resources :cars
  resources :motorcycles
end
于 2013-05-10T12:38:19.987 回答
0

您可以像这样编写嵌套路由:

resources :vehicles do
  resources :cars
  resources :motorcycles
end

这将为您提供以下路线:

/vehicles #index action for vehicles controller
/vehicles/:id #show action for vehicles controller
/vehicles/:vehicle_id/cars #index action for cars controller, with the :vehicle_id param available
/vehicles/:vehicle_id/cars/:id #show action for cars controller, with the :vehicle_id param available
etc.
于 2013-05-10T13:11:41.990 回答