Topic: Fetch XML data into form fields

I want to use Ajax to fill in meta data from Youtube and Vimeo into form fields dynamically after a URL is entered.

As such:

URL : [text_field -- User Input]
Title : [text_field -- User Input & populated by XML]
Tags : [text_field -- User Input & populated by XML]
etc...
All of this would take place with an :onchance for the URL box.

I've written a script to extract the IDs for Vimeo and Youtube, then fetch the XML from their respective sites, I'm just not sure how to populate the form fields dynamically with AJAX then allow the submission to carry those params.

Thank in advance,
-Kyle

Re: Fetch XML data into form fields

So,  you'll have a Youtube url in a form field,  and when the user hits enter,  you want to fire off an AJAX request to Youtube,  and retrieve stuff from Youtube,  and stick it into your model?

Joe got a job, on the day shift, at the Utility Muffin Research Kitchen, arrogantly twisting the sterile canvas snout of a fully charged icing anointment utensil.

Re: Fetch XML data into form fields

Yes but without hitting enter. I would like it to be onchange, then populate the form fields. Essentially, a remote form inside a regular form

Re: Fetch XML data into form fields

Here is how I'd do it.

Your AJAX call from your view will be to an action on the server, let's call it 'go_get_youtube'

the go_get_youtube action will accept ONE parameter,  the youtube URL

This form field is NOT active record based,  so you'd use text_field_tag.  The view with the text_field would look like this:

<%= text_field_tag('utube_url' , nil , 
  :onchange=> remote_function(
    :url=> '/go_get_youtube',
    :with=>"'youtube_url='" + $(this).value()" ))

so now you'd write your go_get_youtube action in your controller,

app/controllers/application_controller.rb:

require 'net/http'
require 'uri'

def go_get_youtube
   url = URI.parse("whatever.youtube.com/#{params[:youtube_url]}") # guessing here
   # OK, I don't know if the youtube API is a post, or a get???, I'll assume get
   req = Net:HTTP:Get.new(url.path)
   resp = Net::HTTP.new(url.host, url.port).start {|http| http.request(req)}
   # at this point,  whatever youtube sends back is in resp.body,  so you do your thing,  again guessing here
   items = parse_out_items(resp.body)
   # at this point, I'll assume items is a hash of the stuff you've parsed out of the youtube response
   # so now you have the data, it's time to use RJS to create JavaScript to send back to the browser to 
   # change your form fields. 
   response :update do |page|
     page['form[title]'] = items["title"]
     page['form[tags]'] = items["tags"]
   end
end

You'll need to add the 'go_get_youtube' route,  something like:

config/routes.rb:

map.connect '/go_get_youtube/:youtube_url', :controller=>'application', :action=>'go_get_youtube'

Last edited by BradHodges (2010-12-13 00:13:29)

Joe got a job, on the day shift, at the Utility Muffin Research Kitchen, arrogantly twisting the sterile canvas snout of a fully charged icing anointment utensil.

Re: Fetch XML data into form fields

Thank you,  I will give it a go tomorrow and post how it went. Also, quick question: I ran into nasty issues installing ferret and acts_as_ferret. Do you have any other suggestions for an index and search plugin?

Re: Fetch XML data into form fields

nope,  sorry,  have no idea about acts_as_ferret,  don't even know what ferret is!!!

Joe got a job, on the day shift, at the Utility Muffin Research Kitchen, arrogantly twisting the sterile canvas snout of a fully charged icing anointment utensil.

Re: Fetch XML data into form fields

Ok, so not going so smoothly.

I get no response in the form, so I made a few tweaks and still get nothing:

<head>
<%= javascript_include_tag :defaults %>
</head>

<h1>New video</h1>

<% form_for :video, :html => {:id => "form"}, :url => 'create' do |f| %>
  <%= f.error_messages %>
  URL: <%= text_field_tag('video_url' , nil , 
    :onchange=> remote_function(
    :url=> {:controller => 'application', :action => 'go_get_meta'}, 
    :with=>'Form.Element.serialize(this)' , #sends video_url = this.value
    :update=>'name')) %><br> #added this to try doing a render text update to test
  Title: <%= text_field_tag 'name' %> 
  <p>
    <%= f.submit "Create" %>
  </p>
  
<% end %>

<%= link_to 'Back', videos_path %>

With my controller

require 'open-uri'
  require 'rexml/document'
  require 'net/http' 
  require 'net/https'
  include REXML


  def go_get_youtube
    @irl = params[:video_url]
    if @irl["youtube.com"]
      @url = @irl[/v=([^&]+)/, 1]
    end
     url = URI.parse("http://gdata.youtube.com/feeds/api/videos/#{@url}") 
     req = Net::HTTP::Get.new(url.path)
     resp = Net::HTTP.new(url.host, url.port).start {|http| http.request(req)}
     # at this point,  whatever youtube sends back is in resp.body,  so you do your thing,  again guessing here
     root = Document.new(resp.read_body).root
     
     render :text => "Hello World" #trying to test the update within the view
     response :update do |page|
       page['form['name']'] = root.elements["title"].text
       
     end
  end

I ran your XML fetch through ruby console and it didn't work. I found this one to be successful. I really just can't seem to get the values to go back to the view.

Re: Fetch XML data into form fields

Ok, realized the action name mismatch but still stuck...