2017-10-19 42 views
0

在我的路由器我有兩個嵌套ressources得到嵌套的ID:Rails的路線 - 如何從URI模式

resources :servers do 
    member do 
    resources :maintenances 
    end 
end 

導致URI模式如下:

  maintenance GET /servers/:id/maintenances/:id(.:format)  maintenances#show 

在maintenance_controller的表演動作我想要得到這些ID如:

@server = Server.find_by(params[:id]) 
@maintenance = Maintenance.find_by ??? 

我的問題是:如何可以在我的maintenance_contr中訪問這兩個ID奧勒從URI模式http://localhost/servers/1/maintenances/1

回答

2

試試這個

resources :servers do 
    resources :maintenances 
end 

然後你就可以訪問嵌套資源作爲跟隨

server_maintenance GET /servers/:server_id/maintenances/:id(.:format) 

在你的控制器

@server = Server.find(params[:server_id]) 
@maintenance = Maintenance.find(params[:id]) 

完整的文檔關於在軌道路由是在official docs