Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Allows you to generate an ActiveResource model ready to interact with the You Tube API.
Branch: master
Pull request Compare This branch is 28 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. The uploaded video can also be updated and deleted. The changes will be reflected at the youtube.


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.

AuthSub for web applications

A link is provided to authorise the website to access to the logged in user’s youtube account.

The token obtained after doing authorisation process will become a session token. So we will set the session parameter to 1 in yaml configuration file. This token is used to upload, edit and delete video.


Please Click <%= link_to ‘here’, youtube_auth_url(authorise_videos_url) %> first to authorise access to your youtube account.

An authorise method will be added to make the single use token to a session token.

def authorise
client =
if params[:token]
client.authsub_token = params[:token] # extract the single-use token from the URL query params
session[:token] = client.auth_handler.upgrade()
client.authsub_token = session[:token] if session[:token]
redirect_to videos_path

Also add ‘include GData’ in the videos controller, which includes the module GData for authorisation process.

  config.gem "hpricot", :version => '0.6', :source => ""
  config.gem 'gdata', :lib => 'gdata'

Listing videos


To get all the videos uploaded by a user you can use the method uploaded_by_user passing the session[:token] we saved during authorisation process


def index
@youtube =YouTube.uploaded_by_user(session[:token]) if session[:token]

def show
@video = YouTube.find_by_id(params[:id]) rescue nil
flash[:message] = “Sorry the video is not found at Youtube” and redirect_to videos_path unless @video

<% if session[:token] >
<= image_tag @youtube.logo %>

<%= @youtube.title %>

<%= “Displaying #{@youtube.startIndex} – #{@youtube.itemsPerPage} of #{@youtube.totalResults}” %> <% 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’, %> <%= link_to ‘Edit/Update’, edit_video_path(video) %>

<%= link_to ‘Delete’, video_path(video), :method => “delete” >
< end >
< end %>


<%= @video.title %>

by <%= link_to, "{}" >
<= youtube_embed @video %>

<%= link_to ‘Back’, videos_path %> <%= link_to ‘Edit/Update’, edit_video_path(@video) %> <%= link_to ‘Delete’, video_path(@video), :method => “delete” %>

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 wich generates an url to the
YouTube authentication page. Pass another url as param to wich be redirected 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:

map.resources :videos, :new => {:upload => :post}, :collection => {:authorise => :get }

def new
@categories ||= YouTube.video_categories

def upload
@upload_info = YouTube.get_upload_url(params[:video])

Also add a link to upload the video at the index page.

<%= link_to “Upload Video”, new_video_url %>
#app/views/videos/new.html.erb <% form_for ‘video’, :url => upload_new_video_path do |f| %> <%= f.hidden_field :auth_sub, :value => session[:token] %> <%= f.label :title %> <%= f.text_field :title %> <%= f.label :description %> <%= f.text_area :content, :rows => 10 %> <%= f.label :category %> <%= :category, @categories %> <%= f.label :keywords %> <%= f.text_field :keywords %> <%= f.submit ‘Continue to Step 2’ %> <% end %> #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 %>

Updating Videos

To get the videos updated by a user you can use the method update_video passing the id of that video, session[:token] and params hash

YouTube.update_video(params[:id], session[:token], params[:you_tube_entry])

before_filter :find_video, :only => [:edit, :update]
def edit
@youtube_videos =YouTube.uploaded_by_user(session[:token])
@video = { |video| == params[:id] }
@youtube = @video.first
@youtube.keywords =
@youtube.category =
@categories ||= YouTube.video_categories

def update
if u = YouTube.update_video(params[:id], session[:token], params[:you_tube_entry]) rescue nil
flash[:message] = “Video has been Updated Successfully.”
flash[:message] = “Video has not been Updated Successfully.”
redirect_to videos_path

private def find_video flash[:message] = “Sorry the video is not found at Youtube” and redirect_to videos_path unless @video = YouTube.find_by_id(params[:id]) rescue nil end

<% form_for @youtube, :url => video_url, :html => { :multipart => true } do |f| >
<= f.label :title >
<= f.text_field :title %>

<%= f.label :description %> <%= f.text_area :content, :rows => 10 %> <%= f.label :category %> <%= :category, @categories %> <%= f.label :keywords %>

<%= f.text_field :keywords %>

<%= f.submit ‘Update video’ %> <% end %> <%= link_to ‘Back’, videos_path %>

h3. Deleting Videos

To delete the videos of any user you can use the method delete_video passing the id of that video and session[:token]

YouTube.delete_video(params[:id], session[:token])

before_filter :find_video, :only => [:show, :edit, :update, :destroy]
def destroy
yt = YouTube.delete_video(params[:id], session[:token]) rescue nil
if yt.msg == “OK”
flash[:message] = “Video has been sucessfully deleted.”
flash[:message] = “Sorry the video has not been deleted.”
redirect_to videos_path

The filter is applied on show method so that method can now be removed.

Also don’t forget to set :multipart => true in the second step.

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.


I’ll really appreciate your feedback, please contact me at vibha[at]


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

Something went wrong with that request. Please try again.