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
- Rails Database Connection Configuration
- Generate a Simple Test App
- Ruby Gems on Your Account
- Resources
- Documentation
Application Setup Instructions
Create your Rails application directory
-
Log in to your Homer, Dante or home.myuw.net account with Tera Term or another terminal emulator.
Press the O key for Other, then press the W key to drop into the Web development environment (Ovid, Vergil or Socrates). cd to public_html.
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
Create a symbolic link to the public folder in your application directory.
ln -s appname-version/public appname
-
Our Rails installation doesn't currently create an .htaccess file, 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
Below is the contents of the .htaccess file that rails should create. You can just copy/paste it into the file you're editing with pico.
# General Apache options AddHandler fastcgi-script .fcgi AddHandler cgi-script .cgi #Options +FollowSymLinks +ExecCGI # 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 # By default the cgi dispatcher is used which is very slow # # For better performance replace the dispatcher with the fastcgi one # # Example: # RewriteRule ^(.*)$ dispatch.fcgi [QSA,L] 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 for your UWNetID and appname should be the name of your application (the same one you used to create the symbolic link).
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.
Open the database configuration file
config/database.yml.pico config/database.yml
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.)
Save the file and exit pico.
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.
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.
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.
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.
Change directories to your Web directory.
cd `wwwhome`
Create a directory to hold the gem(s) you want to install. Mine is called "gemrep".
mkdir gemrep
Install the
sourcesgem to thegemrepdirectory.gem install sources -i gemrep
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
.profilefile.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
.cshrcthat 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.
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
.profileor.cshrcfile.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.
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.
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.
