title: How To Pass Data From Google Drive API Into Rails App Model?
body: <p>There is a simple Rails app form view. The user chooses via a "file-chooser feature" (e.g. Google Picker) a file from his API account (e.g. Google Drive account), and the API sends back data (url, thumbnails, ...) via a javascript callback function (see the JS code below).</p>
  
  <p>How can you pass such data that comes from an API via a javascript callback function into a Rails model?</p>
  
  <p>How does the code look like that ends up in the Rails _form file? What happens in the controller?</p>
  
  <p>Sorry, I am quite new to Rails, and this might be a Rookie question. Any kind of suggestion, links where to read more about it etc. would be really helpful. I was searching for quite a while, but had no luck to find s.th. useful. 
  Thank you so much for helping out! </p>
  
  <p>The html/javascript: </p>
  
  <pre><code>&lt;div class="btn btn-primary" id="gdrive" onclick="javascript:openPicker()"&gt;
  Choose Google Drive File
  &lt;/div&gt;
  
  &lt;script type="text/javascript"&gt;
  
    // The API developer key obtained from the Google Cloud Console.
    var developerKey = '&lt;DEV KEY&gt;';
  
    // Use the API Loader script to load google.picker.
    function openPicker() {
      gapi.load('picker', {'callback': createPicker});
    }
  
    // Create and render a Picker object for searching images.
    function createPicker() {
      var picker = new google.picker.PickerBuilder().
          addView(google.picker.ViewId.DOCS).
          setDeveloperKey(developerKey).
          setCallback(pickerCallback).
          build();
      picker.setVisible(true);
    }
  
    // A simple callback implementation.
    function pickerCallback(data) {
      var url = 'nothing';
      if (data[google.picker.Response.ACTION] == google.picker.Action.PICKED) {
        var doc = data[google.picker.Response.DOCUMENTS][0];
        url = doc[google.picker.Document.URL];
      }
      var message = 'You picked: ' + url;
      document.getElementById('gdrive').innerHTML = message;
    }
  &lt;/script&gt;  
  
  &lt;!-- The Google API Loader script. --&gt;
  &lt;script type="text/javascript" src="https://apis.google.com/js/api.js?onload=loadPicker"&gt;&lt;/script&gt;
  </code></pre>
  
  <p>An example Rails model:</p>
  
  <pre><code>  create_table "postfiles", :force =&gt; true do |t|
      t.text     "gdrive"
      t.integer  "post_id"
    end
  </code></pre>
  
  <p>Again, thanks a lot for reading. Any ideas really appreciated!</p>