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


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, some things may still not work properly, but the main functionality (upload, update, remote video collection retrievals) should work properly)



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 YouTubeModel inherited from ActiveResource::Base 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


All the api finders (except find_by_id) render collections of youtube_model resources. The collection are basically an aray of objects, extended with the following 3 accessors:
- start_index
- items_per_page
- total_results
You can pass any youtube argument to the api finders, or set them as default in your youtube model class

class Video < YouTubeModel::Base
self.default_youtube_options = { :itemPerPage => 10 }
Video.top_rated(:startIndex => 10, :itemPerPage => 30)

uploaded_by_user requires authentication and find_by_id needs it optionally; To use them you have to pass the session[:token] we saved during authorisation process
Accessing the API with theses finders gives you private information about video status (also accessible via @video.status(:token => session[:token])

Video.uploaded_by_user(:token => session[:token])
Video.find_by_id(video_id,:token => session[:token])

def index
@videos= Video.uploaded_by_user(:token => session[:token])

def show
@video = Video.find_by_id(params[:id], :token => session[:token])
flash[:message] = “Sorry the video is not found at Youtube” and redirect_to videos_path unless @video

#app/views/videos/index.html.erb <%= “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 :description, :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


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.