Archive for the ‘Ruby on Rails’ Category

Rails 2 and broken render_text

February 24, 2008

Looks like that in Rails 2, in addition to changing the default database, default sessions store, now some of the oldies such as render_text method no longer work as expected. If you’ve used render_text "some text" in a controller, then you’ll need to replace it with render :text => "some text".

Rails 2 and broken sessions

February 24, 2008

My hosting company (bluehost.com) updates their system software now and then without any e-mail notifications to their users. Unfortunately, if you host a Ruby on Rails application (in my case sharepdf.com) each time they upgrade Ruby on Rails, there is a good chance that something will break. Recently, they upgraded to the latest version of Rails (2.0.2) and needless to say, my Rails application stopped working. It turns out that the latest version of Rails has changed the default sessions store from a file store to a cookie store. The problem is that if you stored anything other than a simple data in your session, then to get your application to work, you have to spend a few days porting your application. The fact that Rails upgrade is not backwardly compatible is really annoying. But that is a separate issue.

So what is the easiest way to port your file backed sessions application to Rails 2? The answer is to use the database for your sessions. But keep in mind that Rails 2 no longer uses MySQL as its default database. Here is how I ported my sharepdf.com application to Rails 2.0.2:

  1. Create a brand new rails application using the command rails -d mysql appname
  2. Edit environment.rb file and explicity state that you wish to use database for sessions: simply uncomment this line:
    config.action_controller.session_store = :active_record_store.
  3. Use the command rake db:sessions:create to create the database definition for the sessions.
  4. Use the command rake db:migrate to actually create the database tables (I’m assuming that you have updated the database.yml with correct user name and password and have create the database in MySQL). The rake command will then create the sessions database table.
  5. Copy your controllers, models, views, … from the your current Rails application to this one.
  6. You should be good to go!

Rails config/environment.rb

October 23, 2007

Today the company I host my sharepdf.com site updated their Rails platform from 1.2.3 to 1.2.5 and all of sudden my Ruby on Rails application that gives users an HTML (suitable for an iPhone) view of the Adobe Share library stopped working. The following error message kept appearing in the error log:


Cannot find gem for Rails ~>1.2.3.0: Install the missing gem with 'gem install -v=1.2.3 rails', or change environment.rb to define RAILS_GEM_VERSION with your desired version.

I had to edit the config/environment.rb file and update the Rails version from 1.2.3 to 1.2.5.


# Specifies gem version of Rails to use when vendor/rails is not present
RAILS_GEM_VERSION = '1.2.5' unless defined? RAILS_GEM_VERSION

This change fixed the problem.

Adobe SHARE for your iPhone

October 2, 2007

Adobe SHAREAdobe today released a beautiful looking online service called Adobe SHARE (beta). Share can be viewed as your online document library in the clouds. You can easily upload a document and share it with others. A thumbnail and an Adobe Flash preview of most document types are automatically created and the Flash preview can be embedded in most blogs. At this point the share service requires a browser with latest version of the Adobe Flash plug-in.

Share PDFIn addition, today Adobe also released the web services for SHARE. The web services offer a simple REST based API to access Share libraries. You can use the web services to create mash-ups. In my quest to learn Ruby on Rails. I’ve written a simple application (based on the RailsSpaces book) that allows you to view your Share library using plain old HTML. I wanted to access my Share library on my iPhone (which has an excellent support for PDF and other file formats). The current mash-uphttp://web.sharepdf.com/ – is not speedy, nothing is currently cached and there are lots of other applications running on this server. You can see the source code here.

There are a couple of minor bugs in the current web services & this mash-up:

  • The XML requests cannot contain any new line characters. For now, just send your XML request as one long string with no line breaks.
  • Ruby returns the current time in seconds. Share API requires the time in mili-seconds, so consecutive calls may appear to be issued at the same time. Just add a sleep in between the calls, e.g., to insert “sleep 1” when you iterate over the list of your documents.
  • There is a typo in the API documentation, the URL to get the source document must end in src and not in source. See the definition of the method src.
  • I tweaked the site.css file and now the application shows up as a tiny thumbnail on my iPhone. Just tap on the app and iPhone will automatically zoom it.

Charting tools

July 19, 2007

Charting in FlashI was looking at creating a chart in Ruby so I started looking at charting libraries. I like the features of dojo, but the dojo documentation is kind of sparse and each quest for information ends up taking too long. Then came along the Flash charing tools of maani.us. It is in Flash (which at least for now means that you wont be able to use it for iPhone). The design of the maani.us is outstanding (it requires Flash 6 or above). Easy charts are quite easy to create, but hard things may be quite difficult to accomplish. For example, I want to create a timeline graph and would like to have a horizontal scrollbar. This is not possible directly in maani.us. I suppose it is possible to embed maani.us in a flash application. I’m still not sure which approach I prefer: Flash based or JavaScript based.

You need to create an XML file to describe your chart. I created the sample maani.us chart using this XML file. Here is the ruby program that created this XML.

Alternating Colors in Ruby/Rails

May 31, 2007

Alternate Colors For displaying lists it is often useful to show the rows of the list in alternating colors. Ruby on Rails makes this really easy.

  1. Define the background colors using HTML style tag.
  2. <head>
    <style>
    .even { background: #99ccff; }
    .odd { background: #cccccc; }

    </style>
    </head>

  3. Add the class attribute that calls the cycle method to the HTML element that you use to describe the row, e.g., DIV or table row TR, …, for example:

    <% for track in @tracks %>
    <div class="<%= cycle('even','odd') %>" >
    <h3> 'show', :id => track %></h3>
    </div>
    <% end %>

That is it. Kudos to Ruby on Rails creators for making this so simple!

Updating Ruby & Rails on OS X

May 28, 2007

The 10.4.9 version of OS X includes ruby 1.8.1-1, and readline 5.0. The latest version of ruby is 1.8.6. You can follow the excellent instructions on maconrails.com I had to make a few minor adjustments to get it to work on my system.

  1. Install xcode for C development. At least you’ll need to install these packages: gcc4.0.pkg, DevSDK.pkg, BSDSDK.pkg, and X11SDK.pkg
  2. The readline that is included in OS X is recent enough.
  3. Create .bash_profile file and modify the PATH environment variable.

    export
    PATH="/usr/local/bin:/usr/local/mysql/bin:/usr/local/sbin:$PATH"
  4. Get the latest version of Ruby and Gem. Copy their folders into /usr/local/src. Now build & install it on your system:

    $ cd /usr/local/src/ruby-1.8.6
    $ source ~/.bash_profile
    $ ./configure --prefix=/usr/local --enable-pthread
    $ make
    $ sudo make install
    $ sudo make install-doc
    $ cd ../rubygems-0.9.4
    $ sudo ruby setup.rb
    $ sudo gem update --system
    $ sudo gem install rails --include-dependencies

Ruby on Rails on OS X

May 27, 2007

Locomotive, TextMateThere are a number of ways to make sure that you’ve what you need to start Ruby on Rails development on OS X. The easiest way is buy a copy of TextMate and install Locomotive (this is all in one Ruby, Rails, Web server bundle). ActiveState sells a full blown IDE (Komodo) with proper debugging support for Ruby. But you need to make sure that you installed rails, … before you can use it.

The 1st application I tried was file upload. Looks like Locomotive’s rails does not understand form_tag_with_upload_progress and this specific feature will require the latest ruby/rails. So now I’ll have to figure out how to update Ruby/Rails on the Mac. FinkCommander here I come.

Ruby bit rot

April 18, 2007

Today I was looking at an earlier test Ruby program that I had written to explore photos on Flickr. I noticed that the program had stopped working and I saw the following error in the log files:

Cannot find gem for Rails =1.1.6: Install the missing gem with 'gem install -v=1.1.6 rails', or change environment.rb to define RAILS_GEM_VERSION with your desired version.

The hosting company that I use updated rails to version 1.2.3 and the rails version that I was using was 1.1.6. I had to edit the file .../config/environment.rb and updated the statement RAILS_GEM_VERSION = '1.2.3' and the program started to work again. Now days it is virtually impossible to write a program that could continue to work over time as the underlying OS, platform, … is revved up.

Rails web application creation steps

January 16, 2007

The Rails framework is an efficient tool for producing web applications using the Ruby language. Here are steps to create a database backed web application. Here are the steps to create a sample application that provides access to a sample UPC database of grocery items. The database was provided as an spreadsheet. Each row of the spreadsheet corresponds to an item and each item has over 80 properties. In this sample, I’ll create a rails application that allows the user to view, find and edit the grocery database. (more…)