嵌套路由有效,但有些则无效

时间:2013-06-26 20:29:42

标签: ruby-on-rails ruby-on-rails-3

在Rails中使用路由进行挣扎!

这有效:http://127.0.0.1:3000/locations/1/statisticshttp://127.0.0.1:3000/locations/不起作用。

我的路线看起来像这样:

resources :locations do
    resources :statistics
end

如果我只是

,我可以 http://127.0.0.1:3000/locations/工作
resources locations

但是嵌套路线不起作用!

我怎样才能让两者兼顾?

非常感谢。

编辑佣金路线:

     location_statistics GET    /locations/:location_id/statistics(.:format)          statistics#index
                         POST   /locations/:location_id/statistics(.:format)          statistics#create
  new_location_statistic GET    /locations/:location_id/statistics/new(.:format)      statistics#new
 edit_location_statistic GET    /locations/:location_id/statistics/:id/edit(.:format) statistics#edit
      location_statistic GET    /locations/:location_id/statistics/:id(.:format)      statistics#show
                         PUT    /locations/:location_id/statistics/:id(.:format)      statistics#update
                         DELETE /locations/:location_id/statistics/:id(.:format)      statistics#destroy
               locations GET    /locations(.:format)                                  locations#index
                         POST   /locations(.:format)                                  locations#create
            new_location GET    /locations/new(.:format)                              locations#new
           edit_location GET    /locations/:id/edit(.:format)                         locations#edit
                location GET    /locations/:id(.:format)                              locations#show
                         PUT    /locations/:id(.:format)                              locations#update
                         DELETE /locations/:id(.:format)                              locations#destroy
              home_index GET    /home/index(.:format)                                 home#index
                   about        /about(.:format)                                      home#about
                 contact        /contact(.:format)                                    home#contact
                    root        /                                                     home#index

编辑2 路由文件

match '/about/' => 'home#about'
match '/contact/' => 'home#contact'

resources :locations do
    resources :statistics
end

get "home/index"

编辑 3

我的实际错误:

Routing Error

No route matches {:controller=>"statistics", :location_id=>nil}

当我去http://127.0.0.1:3000/locations/

1 个答案:

答案 0 :(得分:1)

你应该使用

=link_to "Locations", locations_path

# get sure @location is not nil
=link_to "Location Statistics", location_statistics_path(@location)