1

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.

Ryan K
  • 3,985
  • 4
  • 39
  • 42
  • Nothing to contribute, but interested in this. I highly doubt pluralisation has anything to do with this however. Also noteworthy that this may be a jquery rails thing and not a jquery thing? – Cjmarkham Oct 02 '17 at 21:25
  • Do you have the action set as a `PATCH` request in your routes? – Aaron Eveleth Oct 02 '17 at 21:27
  • It may be that with the extra `s`, there is room to add a value, opposed to without an `s`, where you have already POSTed a value. If this is the case, maybe rails is just treating the `s` one as another POST request. Re-stating my original question, do you have an update function, separate from this, that is set as a `PATCH` request in your routes? – Aaron Eveleth Oct 02 '17 at 21:31
  • @CarlMarkham That's what I'm thinking. @AaronEveleth Yes, I do have an `update` route set as a `PATCH` request. When I have the `s` it goes to that route correctly. Only when I remove the `s` does it revert to a `POST` request (and I don't have an update `POST` route), so it doesn't even make it to my rails code. That's why I think it's a jQuery(-Rails) issue. – Ryan K Oct 02 '17 at 21:37
  • @RyanK It may be worth showing the route path used in the form as well as the relevant route in your routes file. Just for clarifications sake and for future viewers – Cjmarkham Oct 02 '17 at 21:38
  • @CarlMarkham Added – Ryan K Oct 02 '17 at 21:45

0 Answers0