spacerobot
spacerobot

Reputation: 297

Rails - How to set unique id to form select boxes

I have an index page in rails that is outputting the @servicerequest object. I have a dropdown that contains a list of statuses to select from. When I select a change in status it submits the form and updates the record. The problem is that it only inits and works for the first select box on the page.

Here is my form/select box:

    <%= form_with(model: servicerequest, local: true, :html => { :class=>"form-horizontal", id: @servicerequest.id  }) do |form| %>
      <p class="m-b-xs">
        <%= form.select :status, Servicerequest.statuses.keys, {include_blank: true}, class: 'form-control priority' %>
      </p>
    <% end %>

Here is the submit code:

    <script>
    $('.priority').change(function(){
      $('#servicerequest_id').submit();
    return false;
    })
    </script>

How would I assign a unique id to the form/select and reference it in the init code?

Upvotes: 0

Views: 153

Answers (1)

Igor Drozdov
Igor Drozdov

Reputation: 15045

You need to submit the form, which contains the changed select field. For example, $(this).parents("form") selector looks for the parent form.

$('.priority').change(function(){
  $(this).parents("form").submit();
  return false;
})

Upvotes: 2

Related Questions