Testing ssl_requirement with pound

ssl_requirement is a Rails gem by DHH to declaratively redirect requests to SSL.  It is useful, for example, to ensure all access to a checkout page is secure.  SSL encryption, however, is usually handled by a webserver such as Nginx or Apache, and these are not typically used in Rails test mode.  It appears most developers simply disable SSL redirects during testing, but this allows certain defects such as correct handling of flash messages to go undetected.  To test SSL redirects I’ve started using a lightweight, secure reverse proxy called pound. The pound configuration file can be kept to a bare minimum to forward requests from port 8443 to 3000 and encrypt the response:

ListenHTTPS
  Address 127.0.0.1
  Port    8443
  Cert    "pound.pem"
  AddHeader "X_FORWARDED_PROTO: https"
  Service
    BackEnd
      Address 127.0.0.1
      Port    3000
    End
  End
End

To run pound:

pound -vf ./pound.conf

Rails development with multiple MySQL instances

Sometimes supporting multiple clients or projects can result in a need for more than one version of MySQL on the same workstation. This can be a pain, especially with the Ruby native mysql2 gem that can fail with a cryptic ‘failed to allocate memory’ when used with the wrong client libraries.

To address this I’ve started using Bitnami’s RubyStack.  Bitnami’s stacks provide a graphical installer that allows you to quickly specify a non-standard installation location and ports for your developer infrastructure so you can easily install the stack alongside the project being supported.

Instead of using RVM for development I now prepend the RubyStack to my path before working on the supported project:

RUBYSTACK=~/.../rubystack-3.2.1-0
PATH=$RUBYSTACK/mysql/bin:$PATH
PATH=$RUBYSTACK/ruby/bin:$PATH

Be aware the Bitnami stacks are 32-bit. The stacks include graphical and command line tools to start and stop components, see the Bitnami wiki components for more details. Bitnami AIMs are also available on EC2, various deployment options are discussed in the RubyStack README.

Installing Ruby Enterprise Edition on RHEL

RHEL5 is packaged with ruby 1.8.5 which is now outdated. To replace it with REE:

rpm -e `rpm -qa|grep ruby` # remove outdated ruby packages
sudo yum install wget -y
sudo yum install gcc-c++ make patch zlib-devel -y
sudo yum install openssl-devel readline-devel -y
cd /tmp
wget http://rubyenterpriseedition.googlecode.com/files/ruby-enterprise-1.8.7-2011.03.tar.gz
tar xzvf ruby-enterprise-1.8.7-2011.03.tar.gz
sudo ./ruby-enterprise-1.8.7-2011.03/installer --dont-install-useful-gems --auto=/opt/ruby-enterprise
cat > /etc/profile.d/ruby-enterprise.sh <<EOF
if ! echo ${PATH} | /bin/grep -q /opt/ruby-enterprise/bin ; then
PATH=/opt/ruby-enterprise/bin:${PATH}
fi
EOF

. /etc/profile.d/ruby-enterprise.sh

mysql gem installation on Windows x64

Having run into multiple issues setting up MySQL with Rails on Windows x64 my recommendations are:

  • Install the 32-bit version of MySQL, do not try to use the 64-bit version.
  • Install into a path with no spaces, do not accept a default like "C:\Program Files\MySQL\MySQL Server 5.5\"
  • Install the MySQL gem with an invocation similar to:
         gem install mysql -- --platform=ruby --with-mysql-dir="D:\Programs\MySQL\MySQL-Server-5.5" --with-libmysqllib="D:\Programs\MySQL\MySQL-Server-5.5\lib\"
  • Be sure to uninstall/reinstall whenever you install a new version of MySQL.
  • Make sure D:\Programs\MySQL\MySQL-Server-5.5\lib is included in your path, as well as D:\Programs\MySQL\MySQL-Server-5.5\bin. Make sure other copies of libmysql.dll are not being picked up from elsewhere on your path or your ruby installation directories.

Unfortunately with mysql-5.5.15-win32, mysql-2.8.1-x86-mingw32 and Windows 7 x64 even this was not enough: after experiencing a segmentation fault running rake db:create I gave up and downloaded the libMySql.ddl from InstantRails.

    One year with Rails

    One year since I picked up ‘Agile Web Development with Rails’ and coded my first Rails model/controller/view: seems so long ago now.

    Things I’ve mastered in the past year:

    • Test driven development using unit and functional tests.
    • Master-detail forms using Ajax to add and delete rows, fetching product details in JSON for codes entered into a line item.
    • Prototype and jQuery, including jQuery UI datepickers.
    • Loading and parsing XML data files using Nokogiri.
    • Building complex reports using nested views in PostgreSQL, maintained using rails_sql_views.
    • Creating formatted, multi-tab Excel spreadsheets using Spreadsheet gem.
    • Creating pixel-perfect PDF reports using JasperReports loaded using Java-Ruby bridge.
    • Hosting using Apache with Passenger, Mongrel, Thin and Unicorn.
    • Monitoring using ScoutApp.
    • Upgrading a production app from Rails 2.3.4 to Rails 3.0.9.
    • Switching from ERB to HAML.

    Installing Nokogiri on Ubuntu

    Worked for me with Nokogiri 1.4.4 on Ubuntu 10.04 LTS:

    # ruby developer packages
    sudo apt-get install ruby1.8-dev ruby1.8 ri1.8 rdoc1.8 irb1.8
    sudo apt-get install libreadline-ruby1.8 libruby1.8 libopenssl-ruby 
    
    # nokogiri requirements
    sudo apt-get install libxslt-dev libxml2-dev
    sudo gem install nokogiri

    Getting started with Ruby on Windows

    To get started my preference is to use RubyInstaller for Windows.  Both 1.8.x and 1.9.x variants are available. I group together my Ruby variants under \Programs\rubies.

    Having installed multiple variants you can register and manage them using Pik.  I recommend not changing the installation path from the default C:\pik.  It is in fact possible to install Ruby using Pik (by default Pik installs rubies in %HOMEPATH%\.pik\rubies), but I prefer not to do so.

    pik add d:\programs\rubies\ruby-1.8.7-p334\bin
    pik add d:\programs\rubies\ruby-1.9.2-p180\bin

    To access code repositories you will need to download and install msysGit for Windows.  I also strongly recommend TortoiseGit instead of Git GUI.

    To build native extension gems like Nokogiri you will need DevKit.  I tried

    pik package devkit install

    but this did not work for me (got HTTP 404) so I had to manually download and extract the DevKit.  DevKit also includes MSYS/MinGW.

    For an IDE I use Aptana Studio 3.  Studio looks for an existing Git installation or installs its own private copy of portable Git.  Studio then leverages the msysGit bash shell.

    RubyInstaller includes gem, which can then be used to install bundler to complete the setup of the environment.

    RJB on Ubuntu LTS 10.04

    Ruby-Java Bridge (RJB) allows use of Java libraries from within a c-ruby environment, see this slideshow.   RJB provides an alternative when a full port to JRuby is impractical.

    First install Java and build-essential

    apt-get update
    apt-get install openjdk-6-jdk
    apt-get install build-essential

    Now install rjb gem

    export JAVA_HOME=/usr/lib/jvm/java-6-openjdk
    gem install rjb

    The bad news is that RJB currently crashes when used with Passenger so the best alternative is to revert to Mongrel.

    Rails Datepickers

    There are multiple ways to add date-pickers to a Rails application. Searching for a date picker online I came across one Prototype based gem called calendar_date_select that was featured by Ruby Inside, and another one called Calendar-DatePicker-for-Rails.  On closer inspection, however, calendar_date_select was last updated in 2008 and is looking for a new maintainer: the demo still uses Rails 2.2.2: it does not work with some later versions.

    Apart from using a date-picker specifically packaged for Rails it is not that difficult to use a free-standing Javascript solution: I found a nice looking solution called unobtrusive-date-picker but it appears to have limited support. There are also Javascript widget libraries like AUI and jQuery UI.  Finally I came across a Railscast that mentions calendar_date_select but goes on to recommends jQuery UI.  jQuery can be integrated with Rails 2.x using jRails or Rails 3 with jquery-rails and/or jquery-ujs or you can simply forgo the integration and just use jQuery directly. In my case I linked to the libraries hosted by Google like this:

     <%= javascript_include_tag "http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js", "http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.7/jquery-ui.min.js" %>
     <%= stylesheet_link_tag "http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/themes/humanity/jquery-ui.css" %>

    The remaining details of using a jQuery UI datepicker with Rails are covered in the Railscast.

    Using Ruby with Postgres on Ubuntu 10.04 LTS

    Installation of PostgreSQL on Ubuntu is straightforward, see http://library.linode.com/databases/postgresql/ubuntu-10.04-lucid

    To create a simple database with its own user use the command line:

    appenv=myapptst
    psql <<EOF
      create user ${appenv} createdb password '*****';
    EOF
    createdb --username=postgres --owner ${appenv} ${appenv}

    To access the database from Rails use pg

    gem install pg

    Entries in database.yml should look like

    development:
      adapter: postgresql
      username: myappstst
      password: mypwd
      database: myapptst

    Posts navigation

    1 2 3 4 5 6
    Scroll to top