Ruby on Rails

This page is showing a generic answer.
To see a more detailed answer customized for you, type your domain name here:

This page is obsolete.

This page describes how to install Ruby on Rails version 2.2.3. However, this version is outdated, and we no longer support new Rails installations. Customers new to Rails should not use these instructions.

Our company used to support Ruby on Rails. We no longer offer new systemwide versions of it, though, because the Rails market has changed. In particular, the Rails authors have optimized it more for “dedicated servers” or “VPS servers” (which we don’t offer), and current versions of Rails are much more difficult to set up on shared hosting than older versions.

While it’s theoretically possible for technically advanced customers to set up Rails from scratch on our servers using a local copy of Ruby Gems, that’s difficult and we don’t recommend it for most people.

Because of that, we recommend that customers who need Rails support choose a service that is optimized for that, such as the well-regarded Heroku.

The information below is deprecated, but preserved for customers who are continuing to use existing Rails 2.2 installations on our platforms.

On this page:

Creating a Ruby on Rails app

While we unfortunately can't offer support for programming questions, the information below demonstrates how advanced users could create a new Rails 2.2 application.

1. Connect to the server

Make an SSH connection, then change to the html directory:

cd html

2. Create the new Rails application

Rails can automatically create a new application for you. We'll call this one railstest. Just type:

rails --ruby /usr/bin/ruby1.8 railstest

This creates a directory named railstest to contain the application. Change into that directory:

cd railstest

3. Edit "config/environment.rb"

Edit the "config/environment.rb" file using your favorite text editor, such as nano:

nano config/environment.rb

Then add this line near the bottom of the file, just before the line that says "end":

config.action_controller.relative_url_root = "/railstest/public"

This tells Rails the URL we'll be using to access this test site.

4. Put a ".htaccess" file in the "public" directory

The current version of Rails requires that you add a file named ".htaccess" to the "public" directory. The Rails documentation has information about this, but here's a complete, working example .htaccess file to get you started:

AddHandler cgi-script .cgi
Options +FollowSymLinks +ExecCGI
RewriteEngine On
RewriteRule ^$ index.html [QSA]
RewriteRule ^([^.]+)$ $1.html [QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ dispatch.cgi [QSA,L]

After adding that file to the "public" directory, you should be able to go to a URL like http://www.example.com/railstest/public/ in a Web browser and see a working default Rails application. (Enter your domain name in the box at the top of this page to see the exact URL.)

5. Create a new controller

To make your Rails application do something other than the default, create a new controller class. We'll call it "test". Make sure you're back in the "railstest" directory, then type:

script/generate controller test

Among other things, this creates a new file at app/controllers/test_controller.rb.

6. Edit the controller class

Edit the controller file:

nano app/controllers/test_controller.rb

The file initially contains no special code — it looks like this:

class TestController < ApplicationController
end

You can add a few lines of code so it looks like this:

class TestController < ApplicationController
    def hello
        render :text => "Hello World!"
    end
end

After saving that file, you should be able to view a URL like http://www.example.com/railstest/public/test/hello in a Web browser. (Enter your domain name in the box at the top of this page to see the exact URL.)

With the information above, advanced users should be able to make sense of various Rails tutorials available on the Web. If you have questions about Rails, Google's Ruby on Rails group is a great place to start.

Choosing a Rails version

If you create your own new Rails application as described above, the app will "bind" itself to the current installed version of Ruby Gems using RAILS_GEM_VERSION in config/environment.rb. This is not what you want to happen on a server where you don't control the Rails installation.

Instead, you'll want to either "comment out" the RAILS_GEM_VERSION line in the config/environment.rb file (which will make your application always use the latest version of Rails available on the server) or freeze the version of Rails used by your application.

If you're concerned about compatibility, you'll probably want to choose your own version by freezing Rails.

Putting a Rails application at the top level of your site

If you followed the above instructions, you'll notice that the Rails application needs a special URL. However, for a production application, you may not want to include "/railstest/public" as part of the URL.

You can use a separate .htaccess file at the top level of your website to fix this. First create the application as described above, then add these lines to the .htaccess file:

RewriteEngine On

# prevent unauthorized access to files in the Rails directory
RewriteRule ^railstest/ - [forbidden,last]

# rewrite all other requests to Rails, unless a real matching
# file exists
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) railstest/public/$1 [QSA,last]

(Of course, change "railstest" to the actual name of the application you create.)

Using FastCGI with Rails

To speed up production Rails applications, we support FastCGI (and we use mod_fcgid as the Rails documentation recommends).

To enable FastCGI for Rails, first finish debugging your application (using FastCGI during debugging can cause confusing error messages).

Then, make sure the .htaccess file in the public directory of your Rails application contains a "RewriteRule" with "dispatch.fcgi", like this:

RewriteRule ^(.*)$ dispatch.fcgi [QSA,L]

Using "dispatch.fcgi" instead of "dispatch.cgi" makes Rails run about 20 times faster.