Why does my Ruby on Rails custom installation return 404 errors?

If you’ve installed Ruby on Rails manually, or are using an installation from a previous web host, you may find that your Rails pages do not display – instead showing a “404 Not Found” error message.

This is caused by a missing or incorrect .htaccess file in Ruby’s public folder. This file tells the web server how to correctly understand Rails paths.

Fortunately, this is easy to fix, as we have created a simple script you can run to generate the correct .htaccess for Ruby on Rails. If your Ruby installation is in the somedirectory folder under your home directory, then you would connect by SSH and do:-

[yoursite.com@web224 ~]$ cd somedirectory/public
[yoursite.com@web224 public]$ /usr/local/bin/create_rails_htaccess.pl > .htaccess

The create_rails_htaccess.pl script simply outputs a correct .htaccess file, so you can modify the command as you wish (for example, appending it to your existing .htaccess).

How do I set up Ruby on Rails?

Ruby on Rails is a database-driven website framework designed to be easy for programmers to maintain.

To set it up, you will need a hosting package with:-

– SSH access
– 3 MySQL databases (for a standard test, production, live configuration)

The ideal way to install Rails is using our “setup_rails.sh” script, which will correctly configure Rails for MySQL and our web servers.

Once you have logged in to SSH, to install Rails at http://www.yoursite.com/somedirectory you’d do:

[yoursite.com@web224 ~]$ /usr/local/bin/setup_rails.sh somedirectory
[yoursite.com@web224 ~]$ ln -sf `pwd`/somedirectory/public public_html/somedirectory
[yoursite.com@web224 ~]$

You now need to configure Rails to access your MySQL databases. For this you will need to set up 3 databases (by default) using the Web Hosting Plus Control Panel. Note down the names of these, for example you could choose web224-rubyprod, web224-rubytest, web224-rubylive.

Edit the file somedirectory/config/database.yml (NOTE: not database.yaml, as the script will erroneously instruct) and update the database, username and password settings appropriately.

You can now visit http://www.yoursite.com/somedirectory to see the welcome page and confirm the installation has succeeded.

If you want to go on to create a sample page, try:

[yoursite.com@web224 ~]$ cd somedirectory
[yoursite.com@web224 somedirectory]$ script/generate controller welcome
exists app/controllers/
exists app/helpers/
create app/views/welcome
exists test/functional/
create app/controllers/welcome_controller.rb
create test/functional/welcome_controller_test.rb
create app/helpers/welcome_helper.rb
[yoursite.com@web224 somedirectory]$ echo ‘Hello, World! 1 + 2 = <%= 1+2 %>‘ > app/views/welcome/index.rhtml
[yoursite.com@web224 somedirectory]$

You would then be able to view this at http://www.yoursite.com/somedirectory/welcome . For more information, please see the Ruby on Rails web site.

If you want to have your entire site run through Rails (and you’re certain), then rename your public_html directory, and follow the same procedure above except that instead of “ln -sf `pwd`/somedirectory/public public_html/somedirectory” you should write “ln -sf `pwd`/somedirectory/public public_html”.