ideaoforder
ideaoforder

Reputation: 1023

Rails 3.1 routing--collection not working

I recently upgraded to Rails 3.1 (from 3.0), and for some reason one of my routes no longer works. I have a form that posts a csv file, and is processed by an items controller. The route looks like:

  resources :items do 
    member do
      post 'receive'
      post 'show'
    end

    collection do
      post 'csv_import'
      get 'transactions'
      get 'template'
    end
  end

And here's what I see in the logs--it looks like it's posting the correct action.

Started POST "/items/csv_import" for 127.0.0.1 at Tue May 08 11:09:52 -0400 2012
  Processing by ItemsController#show as HTML

But it's being processed by the show action:

ActiveRecord::RecordNotFound in ItemsController#show

Couldn't find Item with id=csv_import

I can't for the life of me see what I'm doing wrong here.

Upvotes: 1

Views: 136

Answers (1)

Dylan Markow
Dylan Markow

Reputation: 124419

Your post 'show' line is interfering with this, because when you post to /items/csv_import, rails thinks you mean items/csv_import/show, with csv_import being the id of the item you want to import. If you run rake routes, you'll see a part like this:

            item POST   /items/:id(.:format)        items#show
csv_import_items POST   /items/csv_import(.:format) items#csv_import

That first item is matching your post to /items/csv_import and it never even hits the second one.

You can move the member do ... end block to be after your collection do ... end block and it should work fine.

However, I would just recommend getting rid of post 'show' and renaming that method to something better, as it goes against the standard rails/rest conventions anyway.

Upvotes: 5

Related Questions