Archive for the 'Ruby on Rails' Category

Installing Phusion’s Passenger on Leopard (OS X 10.5)

This is what I had to do to get Passenger (aka mod_rails) up and running on my Mac. My computer is running OS X 10.5 with a default Ruby and Rails install. The websites I referenced for all the steps are listed at the end.

Prerequisites:
You will need to have XCode installed as mod_passenger.so needs to be compiled.

Installing Passenger Gem

The first thing is to install and build the Passenger gem.

$ sudo gem install passenger
$ sudo passenger-install-apache2-module

The second line will compile mod_passenger.so and launch an installer script. At the end of the script there will be 3 lines that you need to add to your Apache configuration in order for anything to work. Copy these lines directly from the install script as they may be unique to your computer.

Configuring Apache httpd.conf

In this section, we need to configure Apache so it can properly use mod_rails and access your Virtual hosts, which we will setup later. The lines you copied in the previous step need to be added to one of two files - /etc/apache2/httpd-conf or your personal configuration file, in my case /etc/apache2/users/chrisjohnston.conf. it doesn’t matter which one you pick. In getting this all running, I used both files and things worked fine. In the end, I picked my personal config file because it kept things cleaner.

To open the file, use the following:

$ sudo vim /etc/apache2/users/chrisjohnston.conf

This is what my file looks like. I will explain the various parts after the snippet.

LoadModule passenger_module /Library/Ruby/Gems/1.8/gems/passenger-1.0.5/ext/apache2/mod_passenger.so
RailsSpawnServer /Library/Ruby/Gems/1.8/gems/passenger-1.0.5/bin/passenger-spawn-server
RailsRuby /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby

RailsEnv development

<Directory "/Users/chrisjohnston/Sites/">
    Options Indexes MultiViews
    AllowOverride None
    Order allow,deny
    Allow from all
</Directory>

<Directory "/Users/chrisjohnston/Dev/">
    Order allow,deny
    Allow from all
</Directory>

The first three lines load mod_passenger.so and set it up. The RailsEnv development line is needed in order to run the server in development mode. If this is to be a production server, then you can leave this line out as it defaults to production.

The next important part of the file is

<Directory "/Users/chrisjohnston/Dev/">
    Order allow,deny
    Allow from all
</Directory>

This gives you access to all the files below the given directory. This is important if you want things like stylesheets or images to load. We will also come back to this when we set up virtual hosts for the site.

Setting up Virtual Hosts

The next thing to do is to set up a virtual host for your website. In order for this to work, first open the apache httpd.conf file

$ sudo vim /etc/apache2/httpd.conf

and ensure the following line is uncommented, otherwise, nothing else beyond this point will work.

# Virtual hosts
Include /private/etc/apache2/extra/httpd-vhosts.conf

Once you have uncommented line, open up the httpd-vhosts.conf file and add a virtual host pointing to where you rails app is. Also, ensure that NameVirtualHost *:80 is uncommented.

$ vim /private/etc/apache2/extra/httpd-vhosts.conf

Here is what mine looks like:

#
# Use name-based virtual hosting.
#
NameVirtualHost *:80

# Rails apps
<VirtualHost *:80>
    DocumentRoot "/Users/chrisjohnston/Dev/studio_gallery/public"
    ServerName dev.studiogallery.com
    ErrorLog "/Users/chrisjohnston/Dev/studio_gallery/log/error.log"
</VirtualHost>

There may be two dummy virtual hosts listed in the file. Either comment these out or delete them. They will cause errors when you restart the server.

In the snippet above, you can see that I have set up a ServerName for my site. In order to navigate to this (without ending up searching for it on the net) we need to modify the /etc/hosts file. You need to add a line that tells your computer to associate the domain you created with your computer.

# Virtual hosts on this computer
127.0.0.1       dev.studiogallery.com

Now we can restart Apache.

$ sudo apachectl restart

And if everything goes well, when you navigate to your virtual site, your Rails app should come up.

From the highgroove Studio’s The Napkin website, I also added the following to my .bash_profile file:

# Use this in any RAILS_ROOT dir. That restart.txt file tells mod_rails to restart this app.
# You'll want to do this when (for example) you install a new plugin.
alias restart_rails='touch tmp/restart.txt'

# By default, your app's error log now goes here. Unless you configure your apps otherwise,
# it's helpful to have an alias to take you to your error log quickly.
alias apache_logs='cd /private/var/log/apache2/'

# You'll be adding to your vhosts configuration everytime you introduce a new Rails app.
# Might as well make it a shortcut
alias vhosts='sudo vi /private/etc/apache2/extra/httpd-vhosts.conf'

# Dito with hosts
alias hosts='sudo vi /etc/hosts'

# You'll need to restart apache whenever you make a change to vhosts.
# You can also click System Preference->Sharing->Web Sharing, but this is quicker.
alias apache_restart='sudo apachectl restart'

References:

Starting open source github project - Studio Gallery

As part of a larger project, I am starting a new open source Github project - Studio Gallery. It is a Ruby on Rails application aimed at professional photographers.

The app will allow photographers to create galleries for clients, uploading photos from photo shoots. Once the gallery is created, clients can log on and select the photos they want to order. Other features include the ability to rate photos and filter by ratings as well as pick the size of print to be ordered.

The project is just beginning, so nothing exists for it yet except the Github repository. I will putting up a Trac site or Wiki for it soon.

Ruby and Upgrading to Leopard

I recently upgraded my mac to OS X 10.5 and had some problems installing the Ruby gem Hpricot. This particular gem is compiled from source when it is installed (on a Mac at least, no idea about Windows or Linux). When I tried to install it, it kept throwing an error and failing. The problem was that I had forgotten to also upgrade Xcode, so it was being compiled with an old copy of gcc.

Rails user group in Toronto

I found this the other day and thought it might be of interest to Ruby on Rails developers in Toronto. Unspace, a Toronto software development company, is hosting Toronto Rails Pub Nite. They have a mailing list you can sign up on for details and reminders.

What web design app to use for Rails development on OS X?

The subject line basically says it all; what web design application are people using for Ruby on Rails projects on OS X? My first guess is TextMate. Are there any apps out there that support Rails layouts and ERB?

I am currently taking a look at Panic’s Coda and am really impressed with what I am seeing. Anyone have any other suggestions?

Problem installing native Ruby MySQL driver on OS X

In case anyone else has the same problem installing the native Ruby MySQL driver onto their Mac, here is the fix that worked for me.

I was upgrading my install using the HiveLogic instructions and kept getting the following error every time I went to install the mysql gem.

mysql.c: In function 'Init_mysql':
mysql.c:2015: error: 'ulong' undeclared (first use in this function)
mysql.c:2015: error: (Each undeclared identifier is reported only once
mysql.c:2015: error: for each function it appears in.)
mysql.c:2015: error: parse error before numeric constant
mysql.c:2018: error: parse error before numeric constant
make: *** [mysql.o] Error 1

A little Googling later, and I found a solution on the jlaine.net website. There are several other solutions listed in the comments as well.

Rails Tip: Using select_tag with a collection

In Agile Web Development with Rails, they have the following code snippet for working with a selection list and a collection.

<%=
  @users = User.find(:all, :order => "name").map {
    |u| [u.name, u.id]
  }
  form.select(:name, @users)
%>

This may work when using form_for, but I could not get a similar variant to work with form_tag. Instead, I had to do the following:

<%=
  @users = User.find(:all)
  select_tag(:id,
    options_from_collection_for_select(@users, :id, :name))
%>

If you don’t include the options_from_collection_for_select method, you get some very odd HTML output.

<select id="id" name="id">
  John Smith1John Doe2Jane Doe3Jane Smith4
</select>

There may be a simpler way of doing this, but after several hours of searching, this is what I found worked for me.

Next Page »