Ruby & Rails using Passenger

Monday, July 28th, 2008

I have been toying with a MySQL server at work for loading data from a legacy database and creating user accounts in Microsoft’s Active Directory. I am using linux along with MySQL, Ruby and the Rails framework. While I am certainly no über-expert at this, I am finding the task not only manageable, but altogether pleasant. My current workflow looks like this:

  1. Mount Windows share using Samba
  2. Load data into MySQL and perform a bunch of transformations on it
  3. TODO: script creation of Active Directory accounts from MySQL Database (Python??)

I found that I needed to view the data as I was testing the MySQL transformations, so I turned to Ruby and Rails. I have not had a great deal of luck in the past with configuring Apache to serve up dynamic content via Mongrel or lighty, so I decided to give Passenger a go. For those of you that are unaware, Passenger works directly with Apache and requires almost no configuration. It is dead simple to use. So, within a matter of minutes (minus the time to tweak my database to conform to the Rails conventions) I had a web site up and running to display my data as I was testing it!

Open Source Software is becoming (for me at least) a more viable solution to the problems I face than the proprietary alternatives. The wealth of information available and the relative ease with which a great deal of work can be done is amazing.

RoR 2.0

Monday, October 1st, 2007

Via: Ruby on Rails Weblog

Ruby on Rails turns 2.0 (but doesn’t want to celebrate yet). The wonderfully powerful and yet simple to use web framework has reached the 2.0 “preview-release” milestone. According to the venerable DHH, the actual release date for a finalized 2.0 will be some time in the future, after a number of (potential) release candidates. While I don’t understand most of what they have changed, I am happy to see Ruby on Rails moving forward while continuing to gather followers.

Debian + Ruby + Gem + Rails + Mongrel = RubyOnRails

Wednesday, March 21st, 2007

After much frustration, I think I finally have a working installation recipe for RubyOnRails on Debian Linux.

  1. Install Debian
  2. Install ssh and sudo
  3. apt-get install ssh sudo

  4. Remove “root” login from ssh config file
  5. Create a “source” directory
  6. cd /
    mkdir source

  7. Install tools for compiling from various sources
  8. apt-get install gcc gcc-3.4-doc gcc-3.4 g++-3.4 make libc6-dev flex openssl curl wget zlib1g-dev libzlib-ruby

  9. Download latest Ruby (1.8.5 as of this entry), unpack and install
  10. cd /source
    wget http://ftp.ruby-lang.org/pub/ruby/1.8/ruby-1.8.5-p2.tar.gz
    tar xzvf ruby-1.8.5-p2.tar.gz
    cd ruby-1.8.5
    ./configure
    make
    make install
    cd ..

  11. Download, unpack, compile and install zlib (not included in testing version of Ruby?)
  12. wget http://www.blue.sky.or.jp/atelier/ruby/ruby-zlib-0.6.0.tar.gz
    tar xzvf ruby-zlib-0.6.0.tar.gz
    cd ruby-zlib-0.6.0
    ruby extconf.rb
    make
    make install
    cd ..

  13. Download, unpack, compile and install Ruby Gems (0.9.2 as of this entry)
  14. cd /source/
    wget http://rubyforge.org/frs/download.php/17190/rubygems-0.9.2.tgz
    tar xzvf rubygems-0.9.2.tgz
    cd rubygems-0.9.2
    ruby setup.rb

  15. Install Rails
  16. gem install rails --include-dependencies

  17. Install Mongrel
  18. gem install mongrel mongrel_cluster --include-dependencies

  19. Create a dummy application
  20. cd ~/
    rails test
    mongrel_rails start

  21. Go to http://your.ip.address:3000/ to see if it works
  22. Sources

Weblog Development

Thursday, July 6th, 2006

A couple of minor bug fixes:

  1. If you clicked on the word “tags:” at the bottom of each post you were taken to a page listing all of the tags used in this weblog. If you clicked on any of the tags, you were then taken to a page for editing the tags! So now, when you click on a tag from the list of all tags, you will be taken to a page listing all of the entries that have that tag. Still no “tag cloud” but getting closer!
  2. The “updated” problem! When I was making new posts, they were all being listed as “updated”, until I went in to the database and physically changed the “updated_on” date to equal the “created_on” date. This was an error in my original logic, I was under the impression that “updated_on” would stay null until the post was actually updated. It turns out that “updated_on” is set whenever a change is made to the database, even when a new post is created. The fix for this problem was the addition of a column (field) in the posts table that would allow me to flag a post as being updated or not. This is actually more functional than my original approach. Before, if I were to make a minor edit to a post, the post would have considered itself to have been updated. While this wasn’t wholly inaccurate, it was misleading in that most people assume that an updated post has a moe substantive update than the correction of a grammatical error or a simple HTML tweak that doesn’t add anything new to the post. Now, when I edit a post I have to implicity set the “updated” column to display to the user that the post has been updated.

In case anyone is wondering, the second of the two fixes was accomplished easily using Ruby On Rails migration feature. The steps are outlined below

  1. At the command line, enter: (I probably should have called it something like AddUpdatedColumnToPosts!)
    ruby script/generate migration AddUpdatedFieldToPosts
  2. Edit the new migration as follows: (Note that MySQL does not support Boolean columns, but Ruby On Rails know this and handles it for you!)
    class AddUpdatedFieldToPosts < ActiveRecord::Migration
      def self.up
        add_column :posts, :updated, :boolean, :default => false
      end
      def self.down
        remove_column :posts, :updated
      end
    end
  3. At the command line enter:
    rake migrate
  4. Make the neccesary changes in the post controller, in my case I had to change to the logic that determined if the post had been edited or not.:
    <% if updated_on > created_on %>

    to:

    <% if params[:updated] == true %>
  5. Once everything is good on the development side, commit the changes to the Subversion repository:
    svn add * --forcesvn commit -m "fix updated_on problem"
  6. Now it is time to migrate the “Production” database! Log in to the server via the Secure Shell client of your choosing, navigate to your application’s directory and enter:
    rake migrate RAILS_ENV=production -t
  7. If this all goes off without a hitch (it won’t, trust me!), then your final step is to “deploy” your updated application code:
    rake deploy

The beauty of all of this is that I am doing completely disconnected development on my computer at home, once everything works the way I want it to I simply upload my changes to the server, make the neccesary database changes and deploy. If it fails at any point, I can very easily revert to the previous state! I love Ruby On Rails!

This post has been updated to show what an updated post looks like.

RubyOnRails

Thursday, July 6th, 2006

tag cloud

I finally figured out how to create a tag cloud. Yay for me! It was rather difficult, not because the actual implementation was difficult, rather, because I don’t really understand the Ruby programming language yet. I am learning more every day though. I was able to take an example of a tag cloud from craz8’s weblog and work my own solution. I think my solution is a little more elegant because I was able to remove a recursion, hopefully making it faster, but definitely making it look prettier (the code that is).

Here is some code from the helper that builds hash for the tag cloud:

def tag_cloud(tagged_items)

separation = 2
min = tagged_items.values.min.to_f
max = tagged_items.values.max.to_f - min
mult = (seperation / max)

tagged_items.each do |tag, count, fsize|
yield tag, count, ((count - min) * mult) + 1
end

end

Here is the call from the view that displays the tag cloud:

<% @page_title = "tag cloud"%>
<% @page_sub_header = "tag cloud" %>
<% tag_cloud(@tagged_items) do |tag, count, fsize| %>
<%= link_to(h(tag), {:action=> ’show’, :id => tag.id}, {:style => “font-size: #{fsize}em”}) %>
<% end %>

Easy wasn’t it?

Ordering results with acts_as_taggable plugin

Tuesday, June 6th, 2006

You may have noticed that when clicking on the individual tags below any particular post that you will be taken to a list of the most recent posts that also contain that tag. While this is great, it was a little flawed. You see, it wasn’t ordering the results properly! They should be ordered by the date they were posted. Well, as it turns out the fix for this was relatively simple (as most things in Ruby and Rails are).

Original code:

@posts = if tag_name = params[:id]
Tag.find_by_name(tag_name).tagged
else
Post.all_posts
end

Corrected code:

@posts = if tag_name = params[:id]
Post.find(:all, :include => ‘tags’, :conditions => ["tags.name = ?", params[:id]], :order => “posts.created_on DESC”)
else
Post.all_posts
end

Hopefully this helps anybody with a similar problem. When I was working on this I kept running into MySQL errors about an invalid column name, the “?” in the condition was all it took to fix that. I don’t really know what that “?” does, but it works!

My new(ish) weblog

Friday, June 2nd, 2006

So I finally got this weblog up and running on Textdrive. I had run into a whole assortment of problems that I will chronicle in future posts to help alleviate the frustrations of others that may be attempting anything similar. Now I have to upload all of my previous posts from everyday (version 1). Please feel free to comment!

Commenting is not working right now, I am working on it!

This is so Technorati believes that I own this weblog: Technorati Profile

Update on me!

Friday, June 2nd, 2006

Some of you may have noticed that this weblog has been down for a while, so here is a re-cap of things I probably would have written about:

  • I now have a girlfriend, and she is great! There will be more exciting news on this topic in the future.
  • I now have a new job! I will be working for a high school district as a network technician.
  • I finally got my Ruby On Rails powered weblog up an running. There will definitely be more entries on the trials and tribulations I suffered while working on this

RubyOnRails

Monday, January 16th, 2006

Yet another update with no proof! I have worked out many little bugs that were, well, bugging me. The most buggy of them was the tagging. I wasn’t able to display a list of posts by tag if the tag had a space in it. So a tag of “Ruby On Rails” wouldn’t show any posts, but a tag of “RubyOnRails” would (no spaces). So the solution? After some time spent searching through the acts_as_taggable documentation and chatting with some fellow developers on IRC, I found that the following was the problem:

def show_by_tag
@posts = (Post.find_tagged_with(:any => params[:name])).reverse
@tag = params[:name]
end

Here is what I needed:

def show_by_tag
@posts = (Post.find_tagged_with(:any => params[:name], :separator => “+”)).reverse
@tag = params[:name]
end

Note the “:separator => ‘+’”. That is the key. You see when I go looking for a tag, it is listed as “Ruby+On+Rails”, the spaces are replaced with “+” symbols since the browser can’t have spaces in the address bar. So, after I pulled out a great deal of hair, and stayed up way too late I got it working! Yay Me!

Next major step will be testing, getting the code into Subversion for control and getting the new version of this weblog hosted. Keep your fingers crossed.

RubyOnRails

Sunday, January 8th, 2006

I am almost finished re-writing this weblog engine in RubyOnRails! And it is way better to boot. So far I have implemented the following:

  • user authentication
  • access control lists
  • tags
  • comments
  • pretty URLs (sort of)

There has been a good deal of hulabaloo on the web regarding RubyOnRails (RoR). Claims that it makes coding 10 times faster abound, unfortunately I don’t think this is true. Perphaps when I get up to speed with RoR this will be the case, but not so much now. The list of things to do grows shorter, but it still exists:

  • archive list
  • tag cloud
  • gallery