title: How do I update a simple_form input field using AJAX?
body: <p>I have a <em>simple_form</em> input field that is part of a form that I need to update asynchronously when the <em>onchange</em> event is triggered.</p>
  
  <p>The field that I need to update is displayed as a select box since it is a collection. But the collection represents records of a nested set model. So when the user selects a particular value, I want to be able to update the same field with its children and give the user the option of picking any of the children as the value for the same field.</p>
  
  <p>The problem is, how do I update just that field without touching the rest of the form.</p>
  
  <p>Just to give some idea of my current setup:
  The form looks something like this:
  <div class="snippet" data-lang="js" data-hide="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code> &lt;%= simple_form_for @product, html: {class: 'form-horizontal'} do |f| %&gt;
       &lt;%= f.input :product_name %&gt;
       &lt;%= f.association :location, collection: @locations, input_html: { data: {remote: :true, url: "/update_locations"} } %&gt;
   &lt;%= f.submit 'Post', class: 'btn btn-default btn-lg' %&gt;
    &lt;% end %&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p><em>@product is</em> a new <strong><em>Product</em></strong> which <em>belongs_to</em> <strong><em>Location</em></strong>,
  <em>@locations</em> is a collection of <strong><em>Location</em></strong> each of which <em>has_many</em> <strong><em>Product</em></strong>
  hence the use of the <em>simple_form</em> <strong><em>association</em></strong> method
  Location also <em>acts_as_nested_set</em></p>
  
  <p>I have this in my routes.rb</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>get 'update_locations'    =&gt; 'products#update_locations'</code></pre>
  </div>
  </div>
  </p>
  
  <p>I need help in completing the controller action:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>  def update_locations
      @product = Product.new
      @location = Location.find_by_id(params[:product][:location_id])
      @locations = @location.children
      
      # TODO: render code that updates the simple_form field with
      # the new @locations collection
    end</code></pre>
  </div>
  </div>
  </p>
  
  <p>I am not sure what code should go in the partial view that the controller above is supposed to render.</p>