Roberto Pezzali
Roberto Pezzali

Reputation: 2504

Rails Routes: Missing the 'id' in feed routes

I have a problem defining my rails routes.

I have this route:

     resource :feeds, only: [:edit, :destroy] do
      member do
        post 'insert_record'
        delete 'delete_record'
      end
      collection do
        post 'reorder'
      end
    end

And this route

     resources :brands do
      member do
        delete 'destroy_contact'
      end
      collection do
        get 'list', :defaults => { :format => 'json' }
      end
    end

Why in the first case the routes generated are:

GET    /admin/feeds(.:format)          admin/feeds#show
PATCH  /admin/feeds(.:format)          admin/feeds#update
PUT    /admin/feeds(.:format)          admin/feeds#update
DELETE /admin/feeds(.:format)          admin/feeds#destroy

And for brands

GET    /admin/brands/:id(.:format)   admin/brands#show
PATCH  /admin/brands/:id(.:format)   admin/brands#update
PUT    /admin/brands/:id(.:format)   admin/brands#update
DELETE /admin/brands/:id(.:format)   admin/brands#destroy

Why I lose the "id" on feeds? I must add a route to delete a single feed

Upvotes: 0

Views: 88

Answers (1)

tudorpavel
tudorpavel

Reputation: 149

That's because the resource keyword in the case of feeds is singular, while the resources keyword is plural in the case of brands.

Rails has this concept of Singular Resources to define routes for resources that are found not through an id param, but something else, or simply don't make sense to have an id in their route. The guides give the simplest example of a /profile route that always shows the currently logged in user.

Upvotes: 3

Related Questions