NAWTE - A custom WYSIWYG editor for MooTools

New little mootools plugin I’m working on. Check it out! It is based on Control.Textarea for prototype, but re-coded from scratch (and modified a bit) for mootools.

Basically it allows you to put together your custom WYSIWYM (i.e. What You See Is What You Mean) editor with any text area.

Check it out here!

Multiple file upload with: Rails, File_Column plugin and MooTools

Recently, I had to come up with a multiple file upload form that works with the file_column plugin. My site was already using mootools as it’s main JS framework so I wanted to come up with a solution that would be mootools friendly.

Luckily, I found this great mootools plugin that handles multiple file upload in an elegant way, so I decided to start with that as a base.

I had to modify the script, mostly because file_column requires a hidden field for every file field there are in your form. I provided a link to this modified version at the bottom of this post.

Here is a step by step guide to make this work (I am assuming you are familiar with file_column already).

For this example, let’s pretend I have the following models:

  • User (my user model)
  • UserPicture (a picture uploaded by a user)

My UserPicture model has 2 attributes: user_id and image(the file_column). The relationships are as follow: User has_many UserPictures, and UserPicture belongs_to User

I will use 1 view to allow the user to upload pictures. That view will contain the form with the file fields. This is what the view’s code looks like:

  1. <h1>Picture Upload</h1>
  2. <%= javascript_include_tag "Stickman.MultiUpload.js" %>
  3. <script type="text/javascript">
  4.  window.addEvent('domready', function(){
  5.   new MultiUpload( $( 'main_form' ).user_picture, 3, '[{id}][image]', true, true, '[{id}][image_temp]' );
  6.  });
  7. </script>
  8. <form id="main_form" method="post" enctype="multipart/form-data" action="/users/uploadpictures">
  9.  <input name="user_picture" type="file" />
  10.  <br clear="all" />
  11.  <%= submit_tag "Submit" %>
  12. </form>

The important parts are: My file input needs to have the class name as it’s “name” attribute, in this case; user_picture. If your model name is different, change the file input’s name to reflect your model name. Also, if your file_column’s name is NOT “image”, you need to change this line:

  1. new MultiUpload($('main_form').user_picture, 3, '[{id}][image]', true, true, '[{id}][image_temp]');

So it reflects your file_column’s name (change both [image] and [image_temp]) so if your file_column’s name is “picture”, this line would become:

  1. new MultiUpload($('main_form').user_picture, 3, '[{id}][picture]', true, true, '[{id}][picture_temp]');

Next is your controller. In this case, I chose to send the form to “uploadpictures” action of the “users” controller. This is what this controller needs to look like:

  1. def uploadpictures  
  2.  params[:user_picture].each do |id, image|
  3.   @user_picture = UserPicture.new(image)
  4.   @user_picture.user_id = @current_user.id
  5.   @user_picture.save
  6.  end
  7. end

So basically, for each user_picture in the params, I create a new UserPicture model instance, passing the “image” parameter… For my own needs, I have to track the user_id too so the picture is associated to a user. This could be done with a hidden field in the upload form, in my case, sessions are already taking care of it.

And that’s it basically… all the pictures should be uploaded correctly, following file_column’s directory structure, and inserted in the database.

NOTE: In my example, mootools was included in my layout, so I didn’t need to re-include it, but YOU WILL need to include it BEFORE your include the multiple upload script.

To get it working with my modified version, just replace the .js file with mine, and put it in /public/javascripts.

To get the CSS working, I just copied the code of the .css file and added it to my own stylesheet (it shouldn’t overwrite any of your styles but double check to make sure).

And finally, I placed the image that came with it under /public/images/cross_small.gif

The modified version of the Multiple File Upload script can be found here. I suggest you also download the original version here to get the CSS and image file that comes with it.

Best setup for Rails, PHP and MySQL on a small VPS

Anyone who is on a small VPS (256mb in my case) knows how resources can run low pretty quickly! Especially if you are trying to run both a Rails site and a PHP site on it. Pretty much any stack you chose (whether it is nginx with mongrel, or lighttpd with fastcgi or mongrel etc..) uses a considerable amount of memory. Add MySQL to the mix (and most likely a mail server, maybe an ftp too, etc) and you are pretty much at 100% capacity…and your site doesn’t even have any traffic yet…

After trying a bunch of different setup, I can say that for me, LiteSpeed is the winner. Of course no matter which stack you chose, the way you optimize it will always make a big difference. For me, nginx with 1 worker process, 2 mongrel process (using mongrel_cluster) and 1 fcgi process for PHP was using almost 200mb of RAM (with mysql running). Both my rails app and my php site were extremely fast and snappy, even considering the low amount of worker process and mongrel instances. But then again, I had really really low traffic on the sites. Each mongrel process was taking approximately 30mb of memory.

The same sites (1 small rails app, 1 php site) on Litespeed was using approximately 150mb of RAM (approx. 50 less than nginx+mongrel_cluster). The ruby processes uses on average 15mb (instead of 30mb for the mongrel process). The performance seems just fine (although I had a feeling the sites were even faster with nginx, but this isn’t based on any benchmarks, just my personal feeling).

I’ll stick with the Litespeed setup for now until I can find something that uses even less memory.

For a great guide on how to setup Litespeed to serve both PHP and Rails site on Ubuntu, check out the articles at Usefuljaja.

For a great guide on how to setup nginx to serve both PHP and Rails on Ubuntu, check out the tutorial at UrbanPuddle.

If anyone has any suggestion for further optimization I’ll be glad to hear it!

SilverScripting is now at slicehost

Hurray! My (extremely cool!) boss, allowed me to put some of my stuff on his slice over at slicehost. I can already see the performance increase compared to my old VPS… (both were on the same LiteSpeed setup). This is cool especially considering the long waiting list to get a new slice…

Time to move some more stuff over at the slice :)

moodows has a new home

Since I didn’t have much free time to work on moodows anymore, Ben Hutchins offered to take the project over. He had already added a lot of features. I might work some more on the project eventually when I have my life back, in the meantime, check out the new moodows here.

Quick post about Rails DB relationships…

One mistake that I always seem to make is when it comes to choosing between a has_one/belongs_to relationship or simply a belongs_to relationship… I always forget which one should be used in which situation. Well now I’m writing it here so hopefully I remember!

Case 1)
has_one/belongs_to example: Let’s use an example to illustrate when this relationship should be use. So let’s say I have an Address model and a User model, and each user has exactly one DIFFERENT address. In this case I would use the ho/bt relation. This also assumes that the foreign key (user_id) should be stored in the Address table. Simple enough, but I was tricked (again) when I tried using it in a situation where for example each User does have an Address, but not a unique one (i.e. they have to pick from a list of 4-5 addresses). Sure this doesn’t make much sense in this context so let’s go to…

Case 2)
A simple belongs_to relationship. Let’s use a better example than the address one. Last week I was working on snippetslab and I wanted to add “theme” support (a theme is just basically a set of colors to use for syntax highlighting of the snippets). So I though: “Each user should have a theme, therefor, User has_one theme, and theme belongs_to User”. WRONG!… In this case I can’t really store my user_id in the theme table, since I only have 6 themes to offer, and all the users, even though they do HAVE ONE theme, those themes aren’t unique to every users. What needs to be used here seems a little tricky at first, but it makes sense. What I need to do is to store theme_id in my User table (so each user has a theme_id which consit of one of the 6 themes I’m offering) and as for the relationship, all I need to do is to add: belongs_to in my USER model… That’s right. User belongs_to Theme. No need to write anything in my theme model. And this took care of the problem!

It makes sense when you think about it, it’s just the syntax that makes it a bit confusing (At first you are tempted to think that A user has_one theme and a theme belongs_to a user, which is pretty much the case in theory…but in practice, well, it’s different!).

So basically to some it up: If model1 has_one (and EXACTLY ONE, UNIQUE) model2, and foreing key is stored in model2, use has_one/belongs_to. If model1 has one model2 (out of a fixed set, i.e. not one unique model2 per user), and your foreign key is stored in model1, then, model1 belongs_to model2 and that’s it.

Well hope this can help out anyone who is confused with the has_one/belongs_to relation!

Netbeans Ruby IDE kicks ass!

I decided to give netbeans ruby ide a try for my new rails project, after having read a lot about it on blogs (especially this post on LifeOnRails). At first I thought it was a bit slow, but it turns out the nightly build I was using had some problems. I downloaded a new one (like 20 minutes later, yes, new build are coming out quite frequently!), and it was much better.

I have to say I was really impressed by the new NetBeans! I won’t go into details about it’s features here, the post on LifeOnRails did a pretty good job at that! But I’ll say this: If you haven’t tried it yet, you should!

Announcing SnippetsLab

Well it started mostly as a fun project for trying out Rails, and it ended up SnippetsLab . Basically it’s a site where you can post your code snippets. It supports syntax highlighting for 13 languages (more comming), and you can select between multiple “themes”, which will change the colors of the syntax highlighthing (inspired by a similar textmate theme in most case), again, more themes are to be expected soon…

Check it out! You can join for free, save your snippets, post’em on your site etc…

Pamoorama - Panorama for MooTools

Here’s a new plugin for mootools! It allows you to scroll through your panoramic images in a cool way…

Check it out Here

Testing E Blogging Bundle!

Well I’m having fun with E Text-Editor’s Blogging Bundle… Really Sweet actually! :)

Read More »