Search | Directories | Reference Tools
UW Home > UWIN > Computing and Networking > Web > Web Publishing 

Using Ruby on Rails

Summary

Ruby on Rails is an open-source Web application development framework for developing database-backed web applications in Ruby according to the Model-View-Controller pattern.

Included on this page:

Application Setup Instructions

Create your Rails application directory

  1. Log in to your Homer, Dante or home.myuw.net account with Tera Term or another terminal emulator.

  2. Press the O key for Other, then press the W key to drop into the Web development environment (Ovid or Vergil). cd to public_html.

  3. Run the rails command to create your Rails application directory. If you want to use MySQL to store your application's data, please read the Database Configuration section first.

    rails appname-version

    You need to give your rails application directory a different name than the name you want to use to access it. If you don't want to use a version number, then call it something else. Since you're going to access the application at a different directory name than the one you're using for the application directory, feel free to make this application directory name more descriptive.

Set up the Rails application directory to work with the Apache web server

  1. Create a symbolic link to the public folder in your application directory.

    ln -s appname-version/public appname
  2. You'll need an .htaccess file to make this work, so you can just copy/paste the one below into your application's 'public' directory. Make sure to substitute the appropriate UW NetID and appname. You can open an .htaccess file for editing with the following command:

    pico -w appname/.htaccess

    Here's the .htaccess file:

    # If you don't want Rails to look in certain directories,
    # use the following rewrite rules so that Apache won't rewrite certain requests
    # 
    # Example:
    #   RewriteCond %{REQUEST_URI} ^/notrails.*
    #   RewriteRule .* - [L]
    
    # Redirect all requests not available on the filesystem to Rails
    RewriteEngine On
    
    # If your Rails application is accessed via an Alias directive,
    # then you MUST also set the RewriteBase in this htaccess file.
    #
    # Example:
    #   Alias /myrailsapp /path/to/myrailsapp/public
    #   RewriteBase /myrailsapp
    
    RewriteBase /UWNetID/appname
    RewriteRule ^$ index.html [QSA]
    RewriteRule ^([^.]+)$ $1.html [QSA]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ dispatch.cgi [QSA,L]
    
    # In case Rails experiences terminal errors
    # Instead of displaying this message you can supply a file here which will be rendered instead
    # 
    # Example:
    #   ErrorDocument 500 /500.html
    
    ErrorDocument 500 "<h2>Application error</h2>Rails application failed to start properly"
    
    

    UWNetID should be substituted with your UWNetID and appname should be the name of your application (the same one you used to create the symbolic link).

  3. Now you should try going to your application with a browser and see if it displays the "Welcome aboard" page that means your Rails application is working.

    Your application should be at a URL like http://accountType.washington.edu/UWNetID/appname where accountType is staff, faculty, depts, courses or students, and UWNetID is your UW NetID.

Rails Database Connection Configuration

The database configuration for a Rails application is stored in appdir/config/database.yml. There are separate configuration sections for development, test, and production environments.

Rails now defaults to using the file-based database SQLite, which shouldn't require any configuration. If you want to use SQLite, stop reading this section and work on something else. You don't need to do any database configuration.

It is also easy to use MySQL, as this section will demonstrate. When you're creating your Rails application with the rails command, use the -d mysql argument to create a database.yml file for MySQL. Here's the command:

rails -d mysql appname-version

The default environment is development, so you can start by configuring that environment. You can use the same methodology to configure test and production environments.

I'll assume that we're starting in the Rails application directory.

  1. Open the database configuration file config/database.yml.

    pico config/database.yml
  2. Fill out the development section of the database configuration file. Remember to add a line specifying the port after the line "host:".

    development:
      adapter: mysql
      database: (The name of the database you want the app to connect to)
      username: root (or whatever user you want to connect as)
      password: (The database password for the user specified on the line above)
      host: (ovid.u.washington.edu or vergil.u.washington.edu, depending on where you are running MySQL.  Is the account staff/faculty/course/depts or student?)
      port: (You will have to add this line.  It should be the port you're running MySQL on.)
  3. Save the file and exit pico.

  4. That should do it. Write to help@u.washington.edu if this doesn't work.

Generate a simple test app

Rails has a feature called generators that makes it really easy to create simple starting points for a Web application. This section of the instructions uses a generator called "scaffold" to create a simple application.

This section assumes that you have already set up a rails application and done any necessary database configuration.

  1. While in your application directory, run the commands below.

    script/generate scaffold test_row number:integer name:string desc:text
    rake db:migrate

    If these commands generated any errors, take note of them. Any errors likely indicate an incorrect configuration or system problem.

  2. Those commands should have created a simple application and written some tables to the database to support the application. Now you can test the application to make sure it works.

    Go to the URL http://AccountType.washington.edu/UWNetID/appname/test_rows, making sure to replace the AccountType, UWNetID, and appname with the appropriate values for your situation.

  3. Try adding some test rows, editing them and deleting them. If these things work, then you just made a Rails application!

Ruby Gems on Your Account

It is possible to request that Ruby gems be installed system-wide, but this process usually takes a while. You can request software be installed using the Software Request Form. If you want to use a gem right away, you can install it on your own account using these instructions.

  1. Change directories to your Web directory.

    cd `wwwhome`
  2. Create a directory to hold the gem(s) you want to install. Mine is called "gemrep".

    mkdir gemrep
  3. Install the sources gem to the gemrep directory.

    gem install sources -i gemrep
  4. Set the GEM_HOME environment variable to the path of the directory you just created for gems in your Web directory.

    If you're using the bash shell, then you should can set the GEM_HOME environment variable by adding the following line to your .profile file.

    export GEM_HOME="/hw03/d20/UWNetID/gemrep"

    Since I use the tcsh shell, I use a different method. This method also applies to the csh shell. Here's the line in my .cshrc that I use to set this environment variable:

    setenv GEM_HOME "/hw03/d20/UWNetID/gemrep"

    Replace /hw03/d20/UWNetID with the path to your Web directory, which you can find using the wwwhome command.

  5. Log out and back in, or set the environment variable for your current shell.

    If you don't want to log out and back in to your account, you can set the environment variable by executing the same line that you just put in the .profile or .cshrc file.

  6. Install the gem(s) you want to use.

    gem install gem_name

    Make sure to use the name of the gem you want to install, rather than gem_name.

    Some gems have dependencies, so you may have to install gems other than the one you're trying for in order to get it to install.

  7. You can list the gems that are installed on your account using the following command:

    gem list --local

    The output of this command should include the gem(s) you just installed.

  8. In order to get your Rails applications to use the gem repository that you just set up, you'll have to add a line to the public/dispatch.cgi file.

    Add the following line to public/dispatch.cgi right after the first line:

    ENV['GEM_PATH'] = "/hw03/d20/UWNetID/gemrep"

    Make sure to replace the highlighted portion of the path with the path to your gem repository.

    Now your Rails application should be using your new gem repository.

Resources

Documentation