ale
ale

Reputation: 11800

Nested routes work but others don't

Struggling with routing in Rails!

This works: http://127.0.0.1:3000/locations/1/statistics but http://127.0.0.1:3000/locations/ does not work.

My routes look like this:

resources :locations do
    resources :statistics
end

I can get only http://127.0.0.1:3000/locations/ working if I just do

resources locations

but then the nested routes don't work!

How can I get both working?

Many thanks.

EDIT rake routes:

     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

EDIT 2 routes file

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

resources :locations do
    resources :statistics
end

get "home/index"

EDIT 3

My actual error:

Routing Error

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

when I go to http://127.0.0.1:3000/locations/

Upvotes: 0

Views: 62

Answers (1)

BvuRVKyUVlViVIc7
BvuRVKyUVlViVIc7

Reputation: 11821

You should either use

=link_to "Locations", locations_path

or

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

Upvotes: 1

Related Questions