Nginx custom headers for a Unicorn Rails app

Recently I had to implement an API for a Rails app running on Unicorn being proxied in from Nginx. The problem I had was that my custom header wasn’t being passed through from Nginx.

I had already gone ahead and added to my location directive

proxy_set_header        X_SESSION_KEY $http_x_session_key;

But alas I still had problems. Turns out there’s little thing called “underscores_in_headers” which needs to be turned on for my custom (with underscores) API header to work.

Simply add it to your main nginx.conf after the http {

underscores_in_headers on;

RVM – no such file to load — openssl

Installed ruby 1.9.2 via RVM and getting the following error?

no such file to load -- openssl

Try getting RVM to install the required openssl files. Unfortunately this means removing ruby and installing it again.

rvm pkg install openssl
rvm remove 1.9.2
rvm install 1.9.2 --with-openssl-dir=$HOME/.rvm/usr

But hey, it works.

Running Resque Workers from Upstart + RVM

I recently had the task of getting resque workings to start from Ubuntu’s upstart daemon.

First I created a bash script with the following;


# Load RVM into the shell
source "$HOME/.rvm/scripts/rvm"

# Switch to the right RVM version
rvm 1.9.2

# CD to the rails app
cd /home/myapp/

# Start the resque workers
QUEUE=* /home/myapp/.rvm/gems/ruby-1.9.2-p180/bin/rake resque:work

Put the above contents in a file called in your home directory.
Then give it executable status!

chmod +x

Next, create a file called resque.conf in /etc/init (you’ll need sudo access).

description "my App resque workers
start on (net-device-up and local-filesystems)
stop on runlevel [016]
exec su -c "/home/myapp/" myappuser >> /home/myappuser/resque.log 2>&1

If you run into errors, you can take a peek at /home/myappuser/resque.log to debug the problem.

Good luck! :)

Valum Fileupload +jQuery +csrf

I recently went to implement Valum’s Fileupload script with a Ruby on Rails jQuery project I was working on.

One thing I figured out is that all requests in Rails3 must send also send the X-CSRF-Token. Now normally the rails.js jQuery adapter takes care of this; but Valum’s fileupload script is written in raw javascript :)

The problem I had was a logged in user was automatically logged out whenever they attempted a fileupload (I was using the Devise auth gem).

Find line 1203 and add the following below it.

        var token = $('meta[name="csrf-token"]').attr('content');
        if (token) xhr.setRequestHeader('X-CSRF-Token', token);

This will ensure that fileupload requests also send the csrf token.

Ruby On Rails AJAX File Upload With Valum

Some sample code I wrote with Andrew Valums’s AJAX uploader script and Ruby On Rails.

Note; I use the prototype javascript library to manipulate the DOM during the onComplete event.

If you need any help getting this to work, simply use the comments below.

  function createUploader(){            
      var uploader = new qq.FileUploader({
          allowedExtensions: ['jpg', 'jpeg', 'png', 'gif'], 
          element: document.getElementById('file-uploader-demo1'),
          action: '/vendor/products/12/upload',
          params: {
                 postid: '9999'
          onComplete: function(id, fileName, responseJSON){
            if (responseJSON.success) {
              $$('.qq-upload-failed-text').first().update('Successfully Uploaded!');
              $$('.qq-upload-failed-text').first().update('Hmm .. upload failed');
  // in your app create uploader as soon as the DOM is ready
  // don't wait for the window to load  
  window.onload = createUploader;

and the ruby on rails code

  def upload
    @filename = params['qqfile']
    newf ='assets/' + @filename, "wb")
    str = 
    render :text => '{success:true}'

rake db:seed | Don’t know how to build task error

So I ran into a little issue today, I wasn’t able to get my rake db:seed task to work. This is the error I was getting;

rake db:seed  don't know how to build task

The solution?

Update rails to 2.3.5


gem update rails

Will_paginate with multiple models

If you need to implement paging in your Ruby on Rails application, a quick and easy way is to use the fantastic “Will_Paginate” plugin (available here).

However I came across a need to paginate multiple models on the same page, for example; a list of users and a list of groups.

Luckily Google came to the rescue with this article, which demonstrates the use of the :page option, allowing you to specify the page against a model object.

Check out the code below (courtesay of


@users = User.paginate(:page => params[:user_page], :per_page => 10)
@administrators = Administrator.paginate(:page => params[:administrator_page], :per_page => 10)


<%= will_paginate @users, :param_name => 'user_page' %>
<%= will_paginate @administrators, :param_name => 'administrator_page' %>

Capistrano issues – no such file or directory

So I was playing around with my new Slicehost the other day and following their guides to automate a Ruby on Rails deployment process using capistrano.

For those that don’t know, capistrano comes as a ruby gem which allows you to completely automate tasks. In my case, I wanted to remotely tell my Ruby on Rails application to retrieve the latest version of the application from github (remote repository) and then restart my Rails app.

The documentation tells me that a simply “cap deploy” from the root of my rails folder should execute my capistrano script. However for hours upon hours I was banging my head against a wall.

Here’s the error I was receiving;

C:\Web\> cap deploy
* executing `deploy'
* executing `deploy:update'
** transaction: start
* executing `deploy:update_code'
updating the cached checkout on all servers
executing locally: "git ls-remote master"
*** [deploy:update_code] rolling back
* executing "rm -rf /home/xxxx/public_html/releases/20100222103818; true"
servers: [""]
[] executing command
command finished
C:/Apps/Ruby/lib/ruby/gems/1.8/gems/capistrano-2.5.16/lib/capistrano/recipes/deploy.rb:98:in ``': No such file or direct
ory - git ls-remote master (Errno::ENOENT)

The solution

Ok so I’m not really going to call this a solution because I don’t know why this works (and maybe one of you can assist me in this) but running the command “cap deploy” under a MINGW32 command shell instead of the usual Windows command prompt seemed to work just fine.

(I just MINGW32 as a shell to run all my GIT repository commands)

If you don’t use Git on windows or you don’t have MINGW32 installed, I suggest you have a look at this Git on Windows page.

Installing Postgres gem on Windows

So you’re trying to install the postgres gem for ruby on rails, but you’re getting the following message;

Could not find PostgreSQL build environment (libraries & headers): Makefile not created

This is most likely due to the fact that you’re not using the mswin32 gem for Ruby.

Give this a whirl;

gem install ruby-postgres