File Upload: Validating Extensions
I was searching for a way to validate a file against an allowed set of extensions before uploading. My application uses file_column plugin to store video files and I am using mongrel_upload_progress to track the upload. The maximum file size allowed is 50Mb so there was a need to have validation at both client as well as server side.
Model Validation
Client Side javascript validation
mongrel_upload_progress plugin uses following code to submit the form in a hidden iframe. Modify submitUploadForm method as shown:
The advantages of this approach are:
File is validated at both sides to avoid unnecessary uploading of file before model validation. Good for big files.
In case JS is disabled, the file is still validated.
Ajax Tree Plugin
Introduction
This is a helper plugin to generate ajax based tree structure (two level). It can be used for two models having n:m or 1:n relationships. A leaf from any branch can be dragged and dropped in another branch. A scaffolding arrangement is also done to create a new branch, add leaf etc.

For example:
Installation:
Here we take the previous example of Category and Article models. Generate the ajax controller for the two models.
Usage
Example
Copy static files
Copy add.png, delete.png from /vendor/plugins/ajax_tree/ directory to /public/images directory.
Copy ajax_tree.css file from /vendor/plugins/ajax_tree/ directory to /public/stylesheets/ directory.
Include css in your application layout
Call the helpers
Specify model names for branch model and leaf model in /views/home/_show_ajax_tree.rhtml partial.
Then, call this partial in any rhtml, where you want to display the ajax tree. In this case, we have done it in /views/home/index.rhtml
home_controller.rb (Generated automatically)
index.rhtml
Contributions:
This project is hosted at http://code.google.com/p/ajax-tree/. Please leave your feedback.
Flash Movie embed code (RubyOnRails)
Last week I was working on a code to make my video files embedding (flv) available for external webpages. There are many ways to implement this and I have documented a simple way to achieve it.
I am using a Video model with file_column plugin to store the flv file. The path of stored flv files is /public/video/file_path/[id]/[filename.flv] in my application. I am using ffmpeg to convert files to flv format but I am not including this module here.
Here is the controller to access the video files from server filesystem.
To make this url http://server.com/video/index/[id] to something DRY like http://server.com/v/[id], add this mapping to your routes.rb file.
map.connect ‘v/:id’, :controller => ‘video’, :action => ’show’
The Video model with embed_code method looks as follows:
Finally, @video.embed_code gives the code to embed a video as a swf object in external html page.


