Rails3路由STI模型

时间:2013-05-10 11:26:13

标签: ruby-on-rails routes sti

我有Vehicle和STI模型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

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

3 个答案:

答案 0 :(得分: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

答案 1 :(得分:1)

你可以写:

resources :vehicles do
  resources :cars
  resources :motorcycles
end

答案 2 :(得分: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.