So far, it\’s RoR

Ruby on Rails, PostgreSQL, SVN, etc….

Archive for September, 2007

What exactly does the scaffold_resource generator give you?

Posted by Chirag Patel on September 25, 2007

The regular scaffold generator (e.g. ruby script/generate scaffold model controller) requires you to create your model and controller first. The scaffold_resource generator, on the other hand, generates the (RESTful) model and controller for you. For example:

ruby script/generate scaffold_resource Caregiver first_name:string last_name:string

In Rails 2.0, use resource instead:

ruby script/generate scaffold Caregiver first_name:string last_name:string

Here’s everything you get:

Views for all the CRUD operations and a layout

create app/views/caregivers/index.rhtml

create app/views/caregivers/show.rhtml

create app/views/caregivers/new.rhtml

create app/views/caregivers/edit.rhtml

create app/views/layouts/caregivers.rhtml


create app/models/caregiver.rb

Controller with methods for CRUD operations

create app/controllers/caregivers_controller.rb


create app/helpers/caregivers_helper.rb

Looks like this initially

module CaregiversHelper


Migration to create the table in the database

create db/migrate/003_create_caregivers.rb

Looks like this:

class CreateCaregivers < ActiveRecord::Migration

  def self.up

    create_table :caregivers do |t|

      t.column :first_name, :string

      t.column :last_name, :string

      t.column :address, :string

      t.column :city, :string

      t.column :state, :string

      t.column :home_phone, :string

      t.column :work_phone, :string

      t.column :cell_phone, :string

      t.column :relationship, :string

      t.column :email, :string


  end  def self.down

    drop_table :caregivers



Sets up route in config/routes.rb for REST

map.resources :caregivers


Posted in Ruby on Rails | 1 Comment »

Passing parameters to partials

Posted by Chirag Patel on September 12, 2007

Partial basics

Some different things about passing parameters to partials

  1. Basics of passing parameters (i.e. locals) into partials – Why are parameters for partials (locals) passed differently than params for urls?!? Don’t know, but basically use the locals hash in both the render call and use the name of the local in the partial itself. This link also shows how to explicitly check for local parameter in case it’s nil
  2. Making a partial use optional parameters – Basically, set the parameter to nil inside the partial (e.g. title = nil unless defined?(title))
  3. Why passing locals in partials is preferred over @variables – Partials can see the @variables of its parent view, but instead pass the variables as parameters for clarity in complex code bases

Debugging partials

  1. be sure to look at console log (and/or development log), it will show the parameters being passed

Posted in Ruby on Rails | Leave a Comment »

Dynamic styles with HTML, CSS, and Javascript

Posted by Chirag Patel on September 12, 2007

Styles can be dynamically changed in 4 ways:

  • With Javascript (overrides all other styles)

    el = document.getElementById('pageBody'); = 'red';

  • Inline styles (override both embedded and external styles)

    Inline styles are styles that are written directly in the tag on the document. Inline styles affect only the tag they are applied to.

<a href="" style="font-size:91%;">

  • Embedded styles (overrides external styles)

    Embedded styles are styles that are embedded in the head of the document. Embedded styles affect only the tags on the page they are embedded in.

<style type="text/css">
body { color: #FFFFFF; }

  • External styles (.css page)

    External styles are styles that are written in a separate document and then attached to various Web documents. External style sheets can affect any document they are attached to.

<link rel="stylesheet" type="text/css" href="layout.css" />

Posted in HTML/CSS, Javascript | Leave a Comment »

Setting up BackgroundDrb

Posted by Chirag Patel on September 12, 2007

I decided to use BackgrounDrb 0.2.1 to launch a periodic task that would run every 15 seconds.Warning: After going through the hassle of setting up BackgrounDrb, I found out that it isn’t even supported in Windows (my dev environment)! See step 8. So, these instructions haven’t been fully tested yet on a *.nix environment. Good luck and please post comments if you have any helpful suggestions.

  1. Install BackgroundDrb: svn co backgroundrb
  2. Setup BackgroundDrb: rake backgroundrb:setup
  3. Generate worker thread (Foo is capitalized): ruby script/generate worker Foo
  4. In one of your controllers, create new worker with Middleman object (:foo_worker is lowercase with underscore)
       MiddleMan.new_worker(:class => :foo_worker,
        :args => "Arguments used to instantiate a new HeartratepostWorker object",
  5. Instantiate Middleman object in environment.rb (you might not need this with version 0.2.1 of BackgrounDrb)
      require "drb"  DRb.start_service
      MiddleMan =, "druby://localhost:22222"
  6. Install the daemons gem if you haven’t already: gem install daemons
  7. Install the slave gem if you haven’t already: gem install slave
  8. Start the BackgroundDrb server in a separate command window: ruby script\backgroundrb start
    • If you get the error “ERROR: there is already one or more instance(s) of the program running”, delete log\
    • Windows users are out of luck! You’ll get this message: `fork': the fork() function is unimplemented on this machine. Here’s a message from Ezra (the devleoper of BackgroundDrb) about Windows support:
      • “Unfortunately that won’t cut it in this case, I wish it would. Slave does more then just fork. It uses ipc and a few other things that just don’t work yet on windows. There is a possibility it could be made to work but right now it does not, even with win32 process (gem). -Ezra Wed Nov 29 16:19:03 EST 2006”
  9. Debugging methods if things don’t work well


  1. You might want to restart the backgroundrb server after you’ve modified the *_worker.rb file to lib/workers. It could cause an error otherwise.
  2. In case you get this error when starting Mongel, copy the vendor/plugins/backgroundrb directory to a temp directory and it will work

    already initialized constant OPTIONS

  3. To set up a periodic task, set up the Middleman object this way:
            session[:job_key] = worker
              :class => :heartrate_post_worker,
              :args => "some arg to do_work",
              :job_key => :simple_schedule,
              :trigger_args => {
                :start =>,
                :end => + 10.minutes,
                :repeat_interval => 15.seconds


  1. Sending emails in the background with ActionMailer and BackgrounDRB
  2. Example using TTL (time to live)

Posted in Ruby on Rails | 2 Comments »

Accessing your application classes (including models) in Rake tasks

Posted by Chirag Patel on September 1, 2007

When writing Rake tasks, to get access to all classes (such as the User model in this example) in your app’s environment, you need to depend on the :environment task shown as follows:

  task :post_user => :environment  do
    end_time =
    start_time = end_time - 60 #subtract 60 seconds
    until start_time > end_time
      start_time = start_time + 15 #increment 15 seconds
      puts "Posting #{start_time}"
      uesr = => 817, :timestamp => start_time)

Posted in Ruby on Rails | Leave a Comment »