I have a Rails app using a form with several nested resources in it. The weird quirk about it is I am using several single select
inputs as one multiple select
input (i.e. Using the same name
across them). I am also submitting this via JavaScript (i.e. remote: true
). The select
html is below:
<select name="order_item[order_item_options_attributes][1][value2][]">(...)</select>
This works fine when I am using this form for create
. However, when I am trying to update
, it reverts to a POST
request instead of a PATCH
. The _method
hidden field is correctly being generated:
<input name="_method" value="patch" type="hidden">
Here is where it gets weird! If I change the name
of my select
inputs to order_item[order_item_options_attributes][1][value2s][]
(notice the s
after value2
), it works!
So it seems like jQuery only allows a PATCH
request if all nested array values are "pluralized". Is this working as intended or just a bug? Or am I missing something? I am using Rails
4.2.8 and jQuery-rails
4.3.1.
Edit:
Here is the relevant routes. It's just your standard nested routes:
resources :orders do
resources :order_items
end
And the output:
order_order_items_path GET /orders/:order_id/order_items(.:format) order_items#index
POST /orders/:order_id/order_items(.:format) order_items#create
new_order_order_item_path GET /orders/:order_id/order_items/new(.:format) order_items#new
edit_order_order_item_path GET /orders/:order_id/order_items/:id/edit(.:format) order_items#edit
order_order_item_path GET /orders/:order_id/order_items/:id(.:format) order_items#show
PATCH /orders/:order_id/order_items/:id(.:format) order_items#update
PUT /orders/:order_id/order_items/:id(.:format) order_items#update
DELETE /orders/:order_id/order_items/:id(.:format) order_items#destroy
Edit 2 (Clarification):
This appears to be a jQuery
issue and NOT a Rails
issue. The reason I say that is I can edit the name
attribute directly in the browser (i.e. Not a refresh or code change) and it will work immediately. So this seems to be something client-side and not server-side.