title: Using Rails model method to fetch and return value
body: <h2>Problem</h2>
  
  <p>I currently have a method in my <code>models/listing.rb</code> which has a method like this: </p>
  
  <pre><code> def self.lookup_info(val)
     # Fetch some data from the internet
     return{ price: value1, title: value2, isbn: val }
   end
  </code></pre>
  
  <p>When users plug in <code>val</code> into <code>views/listings/_form.html.erb</code> when creating a new listing, I want to call <code>lookup_info</code> and have it fill in the rest of the form if a result (not nil) is returned.</p>
  
  <h2>Setup</h2>
  
  <p>controllers/listings_controller.rb</p>
  
  <pre><code>def new
    @listing = Listing.new
  end
  </code></pre>
  
  <p>views/listings/new.html.erb</p>
  
  <pre><code>&lt;%= render 'form', listing: @listing %&gt;
  </code></pre>
  
  <p>views/listings/_form.html.erb (I use div tags in actual code, but don't include below for simplicity)</p>
  
  <pre><code>&lt;%= form_for(@listing, :html =&gt; {class: "form-horizontal" , role: "form"}) do |f| %&gt;
      &lt;%= f.label :isbn, "ISBN" %&gt;
      &lt;%= f.text_field :isbn , class: "form-control" , id: "isbn_field", placeholder: "ISBN (10 or 13 digits - no dashes)", autofocus: true %&gt;
      &lt;%= f.label :price, "Price" %&gt;
      &lt;%= f.text_field :price , class: "form-control" , id: "price_field", placeholder: "Price" %&gt;
      &lt;%= f.label :title, "URL" %&gt;
      &lt;%= f.text_field :title , class: "form-control" , id: "title_field", placeholder: "Title" %&gt;
      &lt;%= f.submit class: "btn btn-primary btn-lg" %&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p>What javascript and rails do I need to make the _form.html.erb update when a user types in <code>users_isbn</code>, I call <code>lookup_info(users_isbn)</code> and then get an updated partial such that the values of the fields are set to the results. </p>
  
  <p><strong>Example of response:</strong></p>
  
  <pre><code>&lt;%= form_for(@listing, :html =&gt; {class: "form-horizontal" , role: "form"}) do |f| %&gt;
          &lt;%= f.label :isbn, "ISBN" %&gt;
          &lt;%= f.text_field :isbn , class: "form-control" , id: "isbn_field", placeholder: "ISBN (10 or 13 digits - no dashes)", value: lookup_info(users_isbn)[:isbn] autofocus: true %&gt;
          &lt;%= f.label :price, "Price" %&gt;
          &lt;%= f.text_field :price , class: "form-control" , id: "price_field", placeholder: "Price", value: lookup_info(users_isbn)[:price] %&gt;
  ... &lt;!-- Same idea for title --&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <h2>Current Start with Javascript</h2>
  
  <p>Here is what I have so far:</p>
  
  <p><code>_form.js.erb</code> (not sure if this would be the right name for the js file)</p>
  
  <pre><code>var isbnField = document.getElementById('#isbn_field').value;
  if (isbnField.length == (10 || 13)){
    var ajaxResponse = $.ajax({
          url: "listings/lookup",
          type: 'GET',
          data: {isbn: $('#isbn_field').val()}
        });
        ajaxResponse.success(function(){
          alert("Success"); # I would actually want to &lt;%= j render #name of form partial with values %&gt;
        });
        ajaxResponse.error(function(){
           alert("Could not find that ISBN");
        });
  }
  </code></pre>