Skip to content


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Allows you to generate an ActiveResource model ready to interact with the You Tube API.
tree: ad0de58f5f

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.

UPDATE: This fork allow youtube model to work as an active resource like. Allowing validation, callback, etc..
The project is steel on going on my side, so something may still not work but the main functionality (upload, update, remote video collection retrievals) should work properly)



Simply as:

rails plugin install git://

Generating the system

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

rails g youtube_model ModelName

This generator will create four files:

  • An ActiveResource based model under app/models directory.
  • A yaml configuration file under config 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
All the api finders (except find_by_id) render collections of youtube_model resources. The collection are basically an aray of object, extended with the folling accessor:
- start_index
- items_per_page
- total_results

You can pass any youtube argument to the api finders, or set them as default in yout youtube model class

class Video
self.default_youtube_options = { :itemPerPage => 10 }

def index
@videos= Video.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


<%= “Displaying #{@videos.startIndex} – #{@videos.itemsPerPage} of #{@videos.totalResults}” %> <% for video in @videos %> <%= video.title > in <= video.category %> by <%= link_to, “{}” %> <%= simple_format truncate(video.content, :length => 100) %> <%= link_to image_tag(video.thumbnail.first.url, :alt => video.title), video_path(video) %> <%= 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, "{}" >
<= raw 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

To upload a new video to youtube, just instanciate the generated class you inherited from youtube model with a token param and save it

map.resources :videos, :collection => {:authorise => :get }
def new
@categories ||= YouTube.video_categories
@video = => session[:token])
def create
@video =[:video])
render :action => :new

Updating Videos

To get the videos updated by a user, find it with find_by_id. You must pass a token option with the google api authsub token
in order retrieve a video event it hasn’t been processed or accepted by youtube (have a look here

def edit
@video = Video.find_by_id(params[:id], :token => session[:token])
def update
if @video.update_attributes(params[:video])
render :action => :update
render :action => :edit
#app/views/videos/edit.html.erb <% form_for @youtube, :url => video_url 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 destroy method on a video instance

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

def destroy
@video = Video.find_by_id(params[:id], :token => session[:token])
if @video.destroy
flash[:message] = “Video has been sucessfully deleted.”
flash[:message] = “Sorry the video has not been deleted.”
redirect_to videos_path


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.