title: A rails app which gets users location using HTML5 geolocation and saves to data base
body: <p>Seems pretty simple but I'm struggling.</p>
  
  <p>Here's what I have so far in the view.
  I display the location coords just to test its working.
  But I also want to persist on Database, hence the ajax call.
  Am I doing this the right way or is there an easier or better way? </p>
  
  <pre><code>&lt;p id="demo"&gt;Click the button to get your coordinates:&lt;/p&gt;
  &lt;button onclick="getLocation()"&gt;Try It&lt;/button&gt;
  &lt;script&gt;
      var x=document.getElementById("demo");
      var lat;
      var long;
      function getLocation()
      {
          if (navigator.geolocation)
          {
              navigator.geolocation.getCurrentPosition(showPosition);
  
          }
          else{x.innerHTML="Geolocation is not supported by this browser.";}
      }
      function showPosition(position)
      {
          lat = position.coords.latitude;
          long = position.coords.longitude;
          x.innerHTML="Latitude: " + lat +
                  "&lt;br&gt;Longitude: " + long;
  
          $.ajax({
              type: 'POST',
              url: 'http://localhost:3000/locations',
              data: { lat: lat, long: long },
              contentType: 'application/json',
              dataType: 'json'
  
          });
      }
  &lt;/script&gt;
  </code></pre>