So for one of my websites, I wanted to allow users the ability to post youtube videos of themselves for there skateboarding. Now after thinking it over I thought of a pretty primitive yet effective way to quickly, and efficiently do it without taking up lots of space or memory.
Its as easy as this. Create a column you want int the correct table called “video”. Example: If you want each User to have their own video you would add a column, you would use a migration file to add a text column to the User table. That way you could now do User.video and it would point to a text object.
Now, for Facebook to properly use a you tbe video you must call the FBML function:
<fb:swf swfsrc="" width="x" height="y" />
Now, the link to the video must be a certain format, and it is NOT the default hyperlink to the actual video. Say you were to search for a video it would look something like this.
http://www.youtube.com/watch?v=cbYrXR87cvE
However, if you put this in as the hyperlink it will NOT work. The correct link that it requires looks a little different. Instead of /watch?v=[video ID] it looks for a /v/[video ID]. Thus the link would have to be http://www.youtube.com/v/cbYrXR87cvE. So, I figure the easiest way to make this for the user is, allow them to use the INCORRECT link to store into your databse. But, inside your Model Helper create this helper function. Call it youtube_embed.
module CrewsHelper
def youtube_embed(video_id)
return video_id.gsub('watch?v=', 'v/')
end
end
What this does is recieves a String video_id (coul dbe named anything) and uses the usefule ruby gsub method to simply replace the “watch?v” with “v/”. This will turn it into the correct hyperlink format to work. Now to actually you the method you would do something like this.
# if the video param for User is not null (i.e. has a link) do this
<fb:swf swfsrc="" width="460" height=425" />
Now, every time that page is loaded, if the user has a youtube video that he put into his video param, it will show up on that page! Pretty straight forward I hope. Enjoy!
-Justin Bryant