Allows you to generate an ActiveResource model ready to interact with the You Tube API.
Switch branches/tags
Nothing to show
Pull request Compare This branch is 1 commit ahead, 39 commits behind edgarjs:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.


YouTube Model

This plugin allows you to interact with the new YouTube API (yes, that means uploading is now featured)
through a simple ActiveResource model.

Patch for Rails 2.1.1

There was a change in Rails 2.1.1 that causes a 406 Not Acceptable HTTP error. There is a patch
to solve this though.

Just apply the patch to your frozen copy of rails:

curl | git am

See details here

Note about wikis and docs

Currently, the wikis are empty. So I’d appreciate if you want to contribute with wikis documentation; just drop me
a line at edgar.js[at]

But, it doesn’t mean there isn’t enough documentation. The full plugin is rdoced so you just can rake rdoc and
check the details.


Find it at



Simply as:

./script/plugin install git://

Generating the system

The plugin includes a generator to create a model based on ActiveResource

./script/generate youtube_model ModelName

This generator will create four files:

  • An ActiveResource based model under app/models directory.
  • A yaml configuration file under config directory.
  • An initializer that loads the configuration file, under config/initializers directory.

And that’s all, but be sure to check the configuration file to set up your Developer and Client Keys.
You can get these keys at the YouTube APIs and Tools page.

Listing videos

Let’s say you generated a model named YouTube. So now you can use that model very similar
to an ActiveRecord model.

To search for videos passing a simple query use the method find

YouTube.find(‘rails conference 2008’)

To get the videos uploaded by a user you can use the method uploaded_by passing the username


And so on. To see the full list of available methods see the Wikis

All methods will return an instance of Entry. The Entry object encapsulates information about a video, playlist,
suscription, contact or other entity. This class is parsed from the <entry> tag of the xml response, wich is the root
tag in all YouTube data API requests. (To see more about the structure of the xml response go
to the YouTube API

The returned model instance has a main mehod: videos, wich is more than an alias for entry.


  1. VideosController
    def index
    @youtube = YouTube.find(‘ruby on rails’, :max_results => 5)
def show @video = YouTube.find_by_id(params[:id]) end
  1. app/views/videos/index.html.erb

    <%= image_tag youtube.logo %></p> <h1><%= @youtube.title %></h1> <p><%= "Displaying #{youtube.startIndex} – #{@youtube.itemsPerPage} of #{@youtube.totalResults}" >

    <= link_to ‘Upload’, youtube_auth_url(new_video_url) >

    <- for video in @youtube.videos >
    <= video.title > in <= >
    by <= link_to, “{}” >
    <= simple_format truncate(video.content, :length => 100) >
    <= link_to image_tag(, :alt => video.title), video_path( >
    <= link_to ‘Watch on youtube’, >


    end -%>
  1. app/views/videos/show.html.erb

    <%= video.title %></h1> <em>by <%= link_to, "{}" >

    <= youtube_embed @video >

    <= link_to ‘Back’, videos_path %>

As you can see, the youtube_embed method is used to display a video.

Uploading videos

As seen in the index example above, there is a helper named youtube_auth_url which generates a url to the
YouTube authentication page. Pass another url as param to be redirected to after authentication.
This helper method takes the session and secure options from the configuration file. See the details of this options
at the YouTube API

Upload process has two steps:

  1. Meta-info for the video.
  2. File choose.

So basically we have two actions in our controller to upload a video. Here’s a quick code example:

  1. routes.rb
    map.resources :videos, :new => {:upload => :post}
  1. VideosController
    def new
    @categories ||= YouTube.categories_collection
def upload @upload_info = YouTube.get_upload_url(params[:video]) end
  1. app/views/videos/new.html.erb
    <% form_for ‘video’, :url => upload_new_video_path do |f| >
    <= f.hidden_field :auth_sub, :value => params[:token] >

    <= f.label :title >
    <= f.text_field :title %>

<%= f.label :description >
<= f.text_area :description, :rows => 10 %>

<%= f.label :category >
<= :category, @categories %>

<%= f.label :keywords >
<= f.text_field :keywords %>

<%= f.submit ‘Continue to Step 2’ %>

<% end %>
  1. app/views/videos/upload.html.erb
    <% form_tag @upload_info[:url], :multipart => true do >
    <= hidden_field_tag :token, @upload_info[:token] >

    <= label_tag :file >
    <= file_field_tag :file >

    <= submit_tag ‘Upload video’ >

    < end %>

Pay special attention to the routes file, I’ve added a new route called :upload so I can use it
in the first step to set the form post url by calling upload_new_video_path.
Also don’t forget to set :multipart => true in the second step.

Very easy isn’t it? Yep, that is all.

And for a more specific speech… the class method get_upload_url receives a hash containing
the meta-info of the first step, and returns a hash with the upload url and token.

For extra information don’t forget to read the wikis
or if you wanna go further just generate the rdoc for the plugin, it will be uber helpful.


I’ll really appreciate your feedback, please contact me at edgar.js[at]

Special Thanks

I’d like to thank Tal Pink for the nice contribution to the plugin. It was the motivation for finish up this first version! :) Thank you very much.


This code is released under Creative Commons Attribution-Share Alike 3.0 license.