Rails: Nested Resource Generates DELETE/PUT Routes Without `:id` Parameters

Created on 19 May 2017  路  3Comments  路  Source: rails/rails

Steps to reproduce

config/routes.rb:

Rails.application.routes.draw do
  resource :orders do
    resource :order_items
  end 
end

rails routes:

                 Prefix Verb   URI Pattern                        Controller#Action
 new_orders_order_items GET    /orders/order_items/new(.:format)  order_items#new
edit_orders_order_items GET    /orders/order_items/edit(.:format) order_items#edit
     orders_order_items GET    /orders/order_items(.:format)      order_items#show
                        PATCH  /orders/order_items(.:format)      order_items#update
                        PUT    /orders/order_items(.:format)      order_items#update
                        DELETE /orders/order_items(.:format)      order_items#destroy
                        POST   /orders/order_items(.:format)      order_items#create
             new_orders GET    /orders/new(.:format)              orders#new
            edit_orders GET    /orders/edit(.:format)             orders#edit
                 orders GET    /orders(.:format)                  orders#show
                        PATCH  /orders(.:format)                  orders#update
                        PUT    /orders(.:format)                  orders#update
                        DELETE /orders(.:format)                  orders#destroy
                        POST   /orders(.:format)                  orders#create

Test project is generated by rails new testa --database=postgresql --skip-system-test --skip-bundle --skip-coffee --skip-puma --skip-yarn.

Expected behavior

From offical guides http://guides.rubyonrails.org/routing.html#nested-resources, it should generates:

DELETE  /magazines/:magazine_id/ads/:id ads#destroy

System configuration

Rails version: Rails 5.1.1

Ruby version: ruby 2.4.1p111 (2017-03-22 revision 58053) [x86_64-linux]

actionpack

Most helpful comment

You've specified singular resources, using resource in your routes instead of resources. If you fix that up, it should be all good.

All 3 comments

You've specified singular resources, using resource in your routes instead of resources. If you fix that up, it should be all good.

@sevenseacat Thank you! It's my mistake.

That happened to me... 馃槣 Thank you for the answer because you helped me too.

Was this page helpful?
0 / 5 - 0 ratings