Question

I am trying to have the below form submit changes and update my cart whenever the select option changes but I cannot seem to get it to work. What am I doing wrong?

<%= form_for(line_item, :html => {:id => "item-id"}) do |f| %>
    <%= f.select :quantity, [[1, 1], [2, 2], [3, 3], [4, 4], [5, 5], [6, 6], [7, 7], [8, 8], [9, 9], [10, 10]], selected: line_item.quantity, onchange: "$('#item-id').submit();" %>
<% end %>
Was it helpful?

Solution

If we take a look at documentation http://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/select we will see, that the select method accepts as options as html options, so as the second hash is not specified, the mentioned option will be ignored. But following will work:

<%= f.select :quantity, [[1, 1], [2, 2], [3, 3], [4, 4], [5, 5], [6, 6], [7, 7], [8, 8], [9, 9], [10, 10]],  { selected: line_item.quantity }, { onchange: "$('#item-id').submit();" } %>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top