0

I have a has_many :through relationship where feed has_many lists.

Routes

resources :feeds do
  member do 
    resources :feed_lists
  end
end

Route I'm trying to hit:

feed_list
   DELETE /feeds/:id/feed_lists/:id(.:format)   feed_lists#destroy

Then when looping through @feed.lists I get incorrect paths:

feed_list_path(list, @feed) = "/feeds/41/feed_lists/41"
feed_list_path(@feed, list) = "/feeds/5/feed_lists/5"
feed_list_path [@feed, list] = "/feeds/41/5/feed_lists/41/5

Obviously what I want is

feed_list_path(list, @feed) = "/feeds/41/feed_lists/5"

Is this not possible with a has_many :through relationship ?

cbron
  • 4,036
  • 3
  • 33
  • 40

1 Answers1

1

Your routes should look more like this:

resources :feeds do
  resources :lists
end

The clue that it's a routing problem is in the route that was generated; both of the parameters are called id, so the same value is put into the URL twice. The correctly generated route should be feeds/:feed_id/lists/:id (note the different parameters: feed_id and id).

You should now be able to do:

feed_list_path(list, @feed)  # => "/feeds/41/lists/5"
feed_list_url(list, @feed)   # => "http://yoursite/feeds/41/lists/5"
url_for [@feed, list]        # => "http://yoursite/feeds/41/lists/5"

See the Rails routing guide for more information.

georgebrock
  • 28,393
  • 13
  • 77
  • 72
  • Yup, this works. That feed_id/_id thing makes sense. Although I had to leave it as resources :feed_lists not just :lists or it would redirect routes back to the actual :lists controller. – cbron Sep 29 '12 at 00:04