Archive for the ‘Programming’ 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!

How to install MySQL on fedora

January 21, 2008
  1. Use yum to install both mysql command line tool and the server:yum -y install mysql mysql-server
  2. Enable the MySQL service:/sbin/chkconfig mysqld on
  3. Start the MySQL server:/sbin/service mysqld start
  4. Set the MySQL root password:mysqladmin -u root password 'new-password'The quotes around the new password are required.

How to tail log file on a SSH site

January 14, 2008

Quite often I need to tail a server log file on a site that I need to login to using SSH. The task of opening a terminal, creating a SSH session, login, and then tail does get tedious. So here comes ssh with the -t option. Here is an example, where you CD to the appropriate folder and then tail the log:


ssh -t root@myserver.com ‘cd /var/log/tomcat/myserver; tail -f catalina.out; $SHELL -i’

How to overlay a Flex control on Google Maps

November 27, 2007

Overlay Flex ControlYou can add a Flex control to a Google Map by taking advantage of DHTML DIVs and z-order tags. Here I’ve defined a Flex control to display the elevation chart associated with a route. In Flex, I specify the opacity of to be 0.5, this means that the chart will be partially transparent. The z-order of the Flex div ought to be higher than the z-order of the Google Map DIV. Here are a few example of the Flex elevation chart control in action: Sheldon Road Trail, Kennedy/Shannon Trail, Pebble Beach bike route, & Los Gatos to San Jose bike route.

Adding the power of Flex to Google Map gives you to option of selecting the best tools for designing a rich user experience.

Smooth Route Animation in Google Maps: panTo vs. setCenter

November 26, 2007

In my first attempt to animate a route on Google Maps, I used the GMap2.setCenter method. This provided for a spotty animation:


As the marker moved along the route, the background map (as you can see) would often not render. Changing to use GMap2.panTo instead of the GMap2.setCenter method fixed this problem and the flickering of the route problem that I was seeing on Safari.

Now the route animation of the Sheldon Road Trail looks a lot better. So does other routes, e.g. the Pebble Beach route, Kenndey/Shannon/Hicks Roads route, or the Stevens Creek Reservoir route.

How to animate a Flex Slider & update Google maps

November 24, 2007

Flex Slider AnimationFlex offers a number of different animation techniques. For my Route Player application, I wanted to offer an animation speed option so that when the user clicks on the Start button, Flex Slider Animation Control the animation starts: the Flex horizontal slider moves to the right, the time-line on the elevation chart moves accordingly, and the Google map gets updated. To see what I mean, click on the thumbnail, and when the Flex application starts, click on the Start button. To make this all work, first you need to define a Flex Sequence control, and include an AnimateProperty that specifies the duration of the animation.


<mx:Sequence id="myMove" target="{slider}">
    <mx:AnimateProperty property="value"
      fromValue="{timeMin}"
      toValue="{timeMax}"
      duration="{selectedItem.data}" />
</mx:Sequence>

Next, you’ll need to extend the HSlider control and override the set value method. See the control Note19HSlider. This extension of HSlider, includes a callback function that is set to the method sliderMove, which gets called each time the value of the slider has been updated, this in turn updated the chart and the Google Map. The code for all of this is here.

(more…)

How to embed Google Maps in Flex

November 22, 2007

Embed Google Map in FlexCreate your Flex application as you would normally do, design the UI and set aside a rectangle in which you wish to display Google Maps. In the HTML wrapper for the Flex application, add a DIV for the Google map, and add JavaScript functions to show or hide this DIV. These functions will be called from Flex when you need to show or hide the map. In this example, I’ve added the Google map to a Flex Accordion control. The map is shown when you click on the tab “Google Map” and is hidden when you click on the other tabs. The trick to get this to work is to use the Flash wmode="transparent" option when the Flash object is embedded in the HTML page. The code for this example is here.

Click here to see this Flex/HTML integration in action. See also this blog entry. Another item of note is that when you click on the tabs and then click on the browser BACK button, the Accordion control / browser does the right thing, i.e. you go to the previous views. I didn’t do anything to enable this. Flash/Flex must have fixed this. Very nice!

How to use GPX bounds to center the map

November 21, 2007

A GPX file has a bounds element that defines the coordinates of the bounding box of all the GPS points in the file. Here is an example:


<gpx version="1.0" ...>
<time>2007-10-24T02:45:26Z</time>
<bounds minlat="37.221354973"
   minlon="-121.987776104"
   maxlat="37.330222065"
   maxlon="-121.892523821"/>
...
</gpx>

Here is how you can use the Google MAP API to center the routes based on the bounds element in the GPX file:


var b = xmlDoc.documentElement.getElementsByTagName("bounds");
var sw = new GLatLng(b[0].getAttribute(”minlat”), b[0].getAttribute(”minlon”));
var ne = new GLatLng(b[0].getAttribute(”maxlat”), b[0].getAttribute(”maxlon”));
var bounds = new GLatLngBounds(sw, ne);
map.setCenter(bounds.getCenter(), 12);

Here is the above code in action: Los Gatos, Stevens Creek Reservoir bike trail

Processing GPX data with Flex

November 19, 2007

GPX files that you capture from your GPS device using GPSBabel are in XML and start with the following tags:


<gpx
  version="1.0"
  creator="GPSBabel - http://www.gpsbabel.org"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns=”http://www.topografix.com/GPX/1/0″
  xsi:schemaLocation=”http://www.topografix.com/GPX/1/0http://www.topografix.com/GPX/1/0/gpx.xsd”
  >
  …

You wont be able to read these GPX files from Flex (2.01) without modifying the namespace definition. Here is the problem that you will run into: when you try to get all of the trkpt elements, using a construct such as gpx..trkpt (or any other method of traversing the XML, e.g., gpx.descendants("trkpt")) the ActionScript program will silently fail. The only to way to get around this is to remove the XML name space declaration

xmlns="http://www.topografix.com/GPX/1/0"

from the start of your XML file. This must be a bug in ActionScript. The same GPX file works fine when you read it from DHTML using Google AJAX mapping API.