Deploying Rails applications on Windows

Due to some legacy issues, and HP workstations being shit that you can’t turn the hardware virtualization on, i had an issue with one client regarding speed and loading time of an application. My common deployment server is running Ubuntu on VMWare or Oracle VBox, passenger + nginx, or apache even. But, this “server” is just a regular machine(people here don’t want to invest in real hardware, or just don’t have the money).

So i had to deploy the app on windows 7, and i will show you how.

First, there are few steps you have to take care of:

1. Install Ruby with the ruby installer http://rubyinstaller.org/downloads/

2. Install Ruby DevKit from the same source(this will be needed for you to compile some native binaries)

3. Install Thin server(gem install thin)

4. Set up dropbox deployment for your application(i know this is monkeypatching as hell, but if it works…) as described on Rob Conery’s blog

5. Test the app and make sure everything is working ok, (run rails server), then thin start -e production, just to make sure everything is ok, and all of your gems work on windows(fix or replace the ones that don’t)

6. Set up the task scheduler(this is a fine piece of software on Win 7 and up) as described  here, to run rails application almost as a windows service( you start with ‘thin start -e production -p 80 -c “path_to_your_app” ‘)

7. Write a nifty batch script that will restart the server, run migrations, compile the assets and whatnot.


cd "path_to_your_app"

bundle exec rake db:migrate RAILS_ENV=production

bundle exec rake assets:precompile

schtasks /End /TN "name_of_your_task_in_scheduler"

schtasks /Run /TN "name_of_your_task_in_scheduler"

8. After you pull to your dropbox synced repository, login to the server and execute the batch file from there to almost automate the deployment process.

I know this is not capistrano, and cap deploy, and it is hacked as it can be, and also, it works, it’s a small client so i don’t expect much trouble with this.

Lesson here: Try as much as you can to deploy your rails server on a linux server, best in a virtual machine. But if HP messes with you and you have some shitty hardware running windows, this is the best way i have found. If anyone has anything better, put it into comments, i’m happy to make my life easier.

References:

1. http://wekeroad.com/danger-danger/git-and-dropbox-sitting-in-a-tree

2. http://unicornless.com/systems-administration/run-thin-as-windows-service

Preventing users from exiting a form on your website with JQuery

I have encountered a problem with users on a webapp that i co-created. The users would click back after entering data in a form, or close the current tab or something. They were so used to client-server workflow, with open connections to the database that prevented that kind of actions. So i got handed the assignment to prevent the users from doing almost anything but submitting on a data entry form without a notice.

It seemed as a hell of a solution, but i managed to make a simple plugin that works on all major browsers, except Opera. Using window.onbeforeunload, we can prevent the user from doing anything that would hurt their unsaved data. you just load the javascript file, put a tag into your forms that you want to protect and it works out of the box with JQuery.

var isSubmitted = false;
window.onbeforeunload = function() {
  var message = "You could have unsaved changes!";
  if ($('form[data-validate-exit="true"]').length > 0)
  {
    if (!isSubmitted)
    {
      return message;
    }
  }
}
document.ready = function() {
  $('form[data-validate-exit="true"]').find('button[type="submit"]').click(function() {
    window.isSubmitted = true;
  });
}

The message doesn’t show on firefox, but a convenient one will show, enough to warn the user on his actions.

The html code in your form should be like this:


<form data-validate-exit="true">

Your html here

</form>

One friendly suggestion if you follow this path, after you implement this on any of your web apps, use Opera for development, or comment the stuff out for your own sake, you will loose your mind clicking on the leave validations.

Speaking at a conference with no time to prepare

I was surprised to know that i am presenting a product this week at the oracle user conference HROUG.hr

A colleague and me were supposed to hold a talk about creating our latest web app, a nursery management app created using Ruby on Rails and Oracle XE. As we thought, the talk was canceled and we were free of all obligation. But no, this saturday, 5 days before the talk, i was reviewing the agenda and found out the talk is on. So what can i do?

I will walk you through the steps, though they shouldn’t be used unless you are in a crunch, and it is not longer than an hour, luckily my talk is only half an hour.

1. Gather as much irrelevant info about the company and the client(our somewhat of a manager sorted that out)

2. If you are the developer, try to talk as much as you can about the technology and its integration( I love Rails so that is no problem )

3. Scrape out the basic workflow of the application you are presenting, user experience and why is something done in that way.

4. Take as much screenshots as it takes

5. Talk about details in the application, and finally leave at least 5 minutes for the audience to ask questions.

I managed to cramp all that into 30 minutes, i will update this post after i’m done thursday morning, to write how the talk went.

Lesson for everyone: Know your talking schedule at least one month before you are doing a talk, this way you can prepare and practice.

Update: The talk went surprisingly well, the hall was almost full, with 24 people listening. I haven’t got my reviews yet, but i feel they will be great for my first conference talk.

Rails 3.1 JQuery-UI

If you are considering the upgrade to Rails 3.1.x and you wish to include the JQuery-UI in your web application follow these simple steps:

Load jquery-ui in your app/assets/javascripts/application.js

//= require jquery
//= require jquery_ujs
//= require jquery-ui
//= require_tree .

Pick and download a nice theme from: JQuery-UI themes site

Copy jquery-ui-1.8.16.custom.css(version number could differ) to your vendor/assets/stylesheets folder as jquery-ui.css (This will save you time later when you are updating or changing the theme) and include it in your app/assets/stylesheets/application.css


*= require_self
*= require jquery-ui
*= require_tree .

Copy images from the images folder to vendor/assets/images

Modify vendor/assets/stylesheets/jquery-ui.css and replace “images/” to “/assets/”

Now that you have everything set up and ready, go ahead and use the fancy JQuery-UI plugins

Readonly checkboxes with JQuery

I have encountered a problem where i needed to disable some checkboxes on my forms. As you know, a disabled html item by design isn’t going to be posted back. So i needed to figure out another solution. So with the disabled tag, we have a readonly tag, which just blurs the checkbox, but lets the user click on it and change the value(i was testing this in Chrome, i don’t know what happens in other browsers).

The solution is:

1. Make your checkboxes readonly by setting the readonly attribute to true

<%= check_box_tag "some_id", :readonly => true %>

2. Put this javascript in your document.load so the click will be overridden:

// Javascript:

$(':checkbox[readonly="readonly"]').click(function() {
return false;
});

// Coffeescript:

$(':checkbox[readonly="readonly"]').click ->
false