Reusing database.yml Sections

In order to reuse database.yml sections for your database connection information, you can do the following in your database.yml file. Notice the use of &development which creates a tag which can be referenced later within your database.yml file. You can also do something like this, which assigns the &login tag which is later referenced within […]

will_paginate plugin – Installing and Sample Code

Installing the will_paginate Plugin You can install this plugin as a gem but in this example, I’ll install it as a plugin so it can travel with the rails application. This disadvantage of installing it as a plugin instead of a gem is that you’ll need to install it as a plugin for each rails […]

Ruby Require VS Load VS Include VS Extend

Here are the differences between Require, Load, Include and Extend methods: Include When you Include a module into your class as shown below, it’s as if you took the code defined within the module and inserted it within the class, where you ‘include’ it. It allows the ‘mixin’ behavior. It’s used to DRY up your […]

How to Write a Singleton Class in Ruby

A Singleton is a Design Pattern which is used to prevent a class from being instantiated more than once. So there can only be one object of that particular type of class. Writing a Singleton Class in Ruby is very easy and much more straight forward than doing it in Java, for example. It uses […]

Flash[:notice] VS Flash.now[:notice]

Flash[:notice]’s message will persist to the next action and should be used when redirecting to another action via the ‘redirect_to’ method. Flash.now[:notice]’s message will be displayed in the view your are rendering via the ‘render’ method.

Rails Draggable Element Example – Scriptaculous Effect

This is a simple example of how to create two separate draggable elements using Scriptaculous Effect draggable_element. Here is what the end result will look like. Each box will be draggable. So once done, you’ll be able to drag each section and each section will remain on the screen where you leave it: Include default […]

Cheap Web Hosting for Rails Applications

If you want to host Ruby on Rails apps on a shared web hosting account and don’t want to spend too much money, consider using a web hosting company like BlueHost. Advantages of using BlueHost to host your Rails app BlueHost currently, as of September 6, 2009, provides the following services. BlueHost offers many more […]

Installing Ruby 1.9.1 on Linux by Source Code

How to Install Ruby 1.9.1 on Linux by Source Code Actually, the following steps will work with any recent version of Ruby, probably. The following instructions assume you’ll be install Ruby under /opt/local/ruby/. If you’d like it installed elsewhere, replace ‘/opt/local/ruby/’ below with your path. login as root user mkdir -p /opt/local/ruby mkdir -p /opt/local/ruby/src […]

nokogiri – Ruby XML Parser Installation Headaches

Installation sudo gem install nokogiri Do you have an old version of libxml2? create a file called test.rb with the following contents: require ‘rubygems’ require ‘nokogiri’ Execute it: ruby test.rb If you see the following: “HI. You’re using libxml2 version 2.6.16 which is over 4 years old and has plenty of bugs. We suggest that […]

Ruby Time formatting and datetime SQL Column

If you’d like to format Time in Ruby, keep in mind that datetime SQL Columns translate to Time objects. So you can use the strftime method of the Time class to format the date/time. For example, assume scheduled_time is a field in your database and it has a sql data type of ‘datetime’. In your […]