ale
ale

Reputation: 11820

Can't get link_to to work on nested resource

There seem to be a few questions like this but I have read all of them but they don't help.

I have this in views/towns/index.html.haml:

= link_to "towns attractions", towns_attractions(town.id)

I have done an inspect on town.id and the id is correct.

My routes seem okay because I can go to

http://127.0.0.1:3000/towns

and I can go to

http://127.0.0.1:3000/towns/1/attractions

when I take my link_to out (because it breaks).

However, I get this error in index.html.haml:

undefined method `towns_attractions'

I have tried all combinations of pluralization with no luck!

My routes:

resources :towns do
    resources :attractions
end

rake routes:

 town_attractions GET    /towns/:town_id/attractions(.:format)     attractions#index
                         POST   /towns/:town_id/attractions(.:format)     attractions#create
  new_town_attraction GET    /towns/:town_id/attractions/new(.:format) attractions#new
               towns GET    /towns(.:format)                             towns#index
                         POST   /towns(.:format)                             towns#create
            new_town GET    /towns/new(.:format)                         towns#new
           edit_town GET    /towns/:id/edit(.:format)                    towns#edit
                town GET    /towns/:id(.:format)                         towns#show
                         PUT    /towns/:id(.:format)                         towns#update
                         DELETE /towns/:id(.:format)                         towns#destroy

All I want is a link that goes to http://127.0.0.1:3000/towns/1/attractions but where 1 is replaced by town.id.

Upvotes: 0

Views: 68

Answers (1)

Ilia Khokhriakov
Ilia Khokhriakov

Reputation: 3687

towns_attractions should be town_attractions_path. That's it.

Upvotes: 2

Related Questions