title: Getting 2 variables from 1 ajax call in ruby on rails
body: <p>I was wondering how you can get 2 variables to update a div tag using an ajax call instead of just 1. My current .js file:</p>
  
  <pre><code>    $(document).ready(function() {
        $("select").change(function() {
          var selected_product_id;
          selected_product_id = $(this).val();
          $.ajax({
            url: "/products/" + selected_product_id,
            type: "GET",
            success: function(data) {
            $("#description").empty().append(data);
            }
          });
        });
      });
  </code></pre>
  
  <p>show.html.erb where i get the data:</p>
  
  <pre><code>    &lt;%= @product.item %&gt;
  </code></pre>
  
  <p>I would like something like this</p>
  
  <pre><code>        $.ajax({
            url: "/products/" + selected_product_id,
            type: "GET",
            success: function(data) {
            $("#description").empty().append(data[1]);
            $("#price").empty().append(data[2]);
            }
          });
  </code></pre>
  
  <p>with</p>
  
  <pre><code>        &lt;%= @product.item %&gt;
          &lt;%= @product.price %&gt;
  </code></pre>
  
  <p>where my description div gets updated with @product.item and my price div gets updated with @product.price. How could I achieve this?</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>updated .js file</p>
  
  <pre><code>    $(document).ready(function() {
        $("select").change(function() {
          var selected_product_id;
          selected_product_id = $(this).val();
          $.ajax({
            url: "/products/" + selected_product_id,
            type: "json",
            success: function(data) {
            $("#description").empty().append(product.item);
            $("#price").empty().append(product.price)
            }
          });
        });
      });
  </code></pre>
  
  <p>show.html.erb:</p>
  
  <pre><code>    &lt;%= @product.item %&gt;
      &lt;%= @product.price %&gt;
  </code></pre>
  
  <p>controller:</p>
  
  <pre><code>    class ProductsController &lt; ApplicationController
      respond_to do |format|
        format.html # show.html.erb
        format.json  { render :json =&gt; @product.to_json }
      end
  
      def index
      end
  
      def show
      @product = Product.find(params[:id])
      end
  
      end
  </code></pre>
  
  <p>I'm pretty sure that controller isn't correct. I placed @product before respond_to and it didn't work so I just put respond_to without really knowing where it goes. Sorry for being such a noob. Thanks for all your help.</p>
  
  <p><strong>FINAL EDIT:</strong></p>
  
  <p>working javascript file:</p>
  
  <pre><code>    $(document).ready(function() {
        $("select").change(function() {
          var selected_product_id;
          selected_product_id = $(this).val();
          $.getJSON("/products/"+selected_product_id,function(data){
        $("#description").empty().append(data.item);
        $("#price").empty().append(data.price);
          });
        });
      });
  </code></pre>