Infotx

Welcome to Infotx - Webmaster Guides and Resources.

Web hosting using Ruby On Rails.

Ruby On Rails

 

What Ruby on Rails gems are usually pre-installed?

 

Most hosting companies currently run Ruby version 1.8.4 and Rails version 1.1.4.

The following gems are installed for Ruby on Rails. If you need a gem installed, please submit a ticket requesting it. Most hosting companies reserve the right to not install gems for security reasons, etc.

actionmailer (1.2.0, 1.1.5)
Service layer for easy email delivery and testing.

actionpack (1.12.0, 1.11.2)
Web-flow and rendering framework putting the VC in MVC.

actionwebservice (1.1.0, 1.0.0)
Web service support for Action Pack.

activerecord (1.14.0, 1.13.2)
Implements the ActiveRecord pattern for ORM.

activesupport (1.3.0, 1.2.5)
Support and utility classes used by the Rails framework.

fcgi (0.8.6.1)
FastCGI ruby binding.

login_generator (1.1.0)
[Rails] Login generator.

mysql (2.7)
MySQL/Ruby provides the same functions for Ruby programs that the
MySQL C API provides for C programs.

rails (1.1.0, 1.0.0)
Web-application framework with template engine, control-flow layer,
and ORM.

rake (0.7.0)
Ruby based make-like utility.

sources (0.0.1)
This package provides download sources for remote gem installation

 

 

How do I set up Ruby on Rails?

 

Using Ruby On Rails

This is intended to be a brief introduction to developing ruby on rails applications. At the bottom of this article you will find a number of resources to help you learn more about ruby on rails and related information, as well as links to some rails tutorials that will go into more depth than this document.

Before you start digging your feet into Ruby on Rails, you should understand exactly what it is. Ruby on Rails is an advanced object-oriented Model-View-Controller application framework. If you didn't fully understand the meaning of the previous sentence, you're going to need to put in some study time before you can jump into rails programming. Ruby On Rails is aimed at advanced programmers; jumping into it before you're ready is likely to be very very hard. This tutorial should be easy enough for anyone to follow, but there's a lot more to rails than you'll be learning here. This tutorial serves as a first step into ruby on rails.

The Model-View-Controller (abbreviated to MVC) design pattern is fairly straight-forward, it simply means that your program is split into three separate components: The Model, View, and Controller. The "Model" is your data, no matter how it's stored. If you're writing a blog, this is where all of your posts and comments would go. The "View" is your interface. In the case of ruby on rails, we're talking about the part that displays your HTML. The controller handles the business logic, and ties the model to the view. MVC programming is beneficial for many reasons.

From this point on it is assumed that you have an understanding of both object-oriented design and MVC, and now you can get into how to develop rails applications. A few additional notes before you start:

First of all, you need to have SSH access enabled. You'll need to contact customer support for this.

Secondly, you'll see a lot of tutorials referring to a program called "script/server" or "webrick". This is NOT NECESSARY on a hosting account, and you should never have to use it. This is designed for people who are developing their rails application on their own computer where there is no apache install which is pre-configured to use ruby on rails. However, you do have access to such a server on hosting accounts, so you do not need to worry about script/server. Do not run it, as it will not even work.

To begin, log into the server using SSH. You'll need a work area for your rails application. Assuming ahead of time that you may eventually want multiple applications, you should make a work directory and then cd into it. You can name it whatever you would like, but this document assumes that it is called "rails".

% mkdir ~/rails
% cd ~/rails

Now you may create your application. As we are just making a simple Hello World application, we'll assume that the application is named "first".

% rails first
% cd first

Next, we're going to set up a subdomain for this application to run on. Log into your cPanel, click on 'subdomains', then type 'first' into the first text box and click 'Add'. You've now created a new subdomain, first.yourdomain.com, which will be the new home of your ruby on rails application. Now, we're going to make your application's "public" directory be the rootdir of that subdomain with the following commands:

% cd ~/public_html/
% rm -r first
% ln -s /home/YOUR_USERNAME/rails/first/public first

You should now be able to go to http://first.yourdomain.com/, where you will see the Ruby on Rails welcome message. As the welcome page suggests, it is now time to set up your databases.

In cPanel, click on 'MySQL Databases'. The first thing you'll want to do here is add an SQL user for rails to use. You can name this whatever you'd like. We will assume you used 'rails'. cPanel prepends your username to the user name, so you should take note of the actual name created (it should be username_rails).

Next, we're adding a database. Name this database 'first', to match your application name. You will again notice that username_ has been prepended. Finally, we're going to link this username to the database. Select username_rails and username_first from the dropdowns and make sure the 'All' checkbox below them is checked, then click the 'Add User To DB' button.

Now you should repeat this step, with 'firstdev' as the database name, and linking username_rails to it.

Now we're going to edit the database.yml file. Open ~/rails/first/config/database.yml in your favorite editor, and modify the 'development' and 'production' sections to contain the username, password, and database that you just created.

production:
adapter: mysql
database: username_first
host: localhost
username: username_rails
password: password

development:
adapter: mysql
database: username_firstdev
host: localhost
username: username_rails
password: password

Next you should create the actual database data. From the mySQL page in cPanel, you should find a 'phpmyadmin' link. Within phpmyadmin, select the "_first" database from the dropdown on the left, then click on the "SQL" tab along the top. Paste the following into your box and click 'Go':

CREATE TABLE `people` (
`id` int(10) unsigned NOT NULL auto_increment,
`name` varchar(50) NOT NULL default '',
`street1` varchar(70) NOT NULL default '',
`street2` varchar(70) NOT NULL default '',
`city` varchar(70) NOT NULL default '',
`state` char(2) NOT NULL default '',
`zip` varchar(10) NOT NULL default '',
PRIMARY KEY (`id`),
KEY `name` (`name`)
) TYPE=MyISAM AUTO_INCREMENT=2 ;

Now, click on the SQL tab one more time, and run this query:

INSERT INTO `people` VALUES (1, 'Superman', '123 Somewhere', '', 'Smallville', 'KS', '123456');

Now repeat these two sql queries for your _firstdev database.

The next step is to create a controller.

% ./script/generate controller First list view new edit

After that has been created, you will create your model.

% ./script/generate model Person

Now we're going to modify two files. First, open app/views/first/view.rhtml and make it look like this:

<html>
<body>
<h1>Friends#view</h1>
<p>This page will display one friend</p>
<p>
<%= @person.name %><br />
<%= @person.street1 %><br />
<%= @person.street2 %><br />
<%= @person.city %><br />
<%= @person.state %><br />
<%= @person.zip %><br />
</p>
</body>
</html>

Next, open app/controllers/first_controller.rb and modify the 'view' method to look like this:

def view
@person = Person.find(1)
end

Congratulations, You now have a working ruby on rails application which reads information from a database. Go to http://first.yourdomain.com/first/view and you should see Superman's information.

You should now go on to read other ruby on rails tutorials. You should also watch the Ruby On Rails Screencasts, which show you, among other things, how an experienced ruby on rails developer can create a fully functional application in a matter of minutes using ruby on Rails.

ADDITIONAL INFORMATION AND TUTORIALS:
Why's poignant guide to ruby: You will either enjoy this or hate it, but it's a nice intro to the ruby language: http://poignantguide.net/ruby

 

 

How do I install my own Gems?

 

 

The easiest and fastest way to install Ruby gems is to install them in your local directory, you will need shell access. To install your own gems use the following steps:

1) Using File Manager in your cPanel make a copy of the .bashrc file in your root directory, name it .bashrc.bak.
2) Now edit the .bashrc file and add the following to the end of the file:

export PATH="$PATH:$HOME/packages/bin:$HOME/.gems/bin"
export GEM_HOME="$HOME/.gems"
export GEM_PATH="$GEM_HOME:/usr/lib/ruby/gems/1.8"

export GEM_CACHE="$GEM_HOME/cache"

3) Using your favorite SSH client connect to your site.



4) at the prompt type:

cp /usr/lib/ruby/gems/1.8/cache/sources-0.0.1.gem ./

gem install sources-0.0.1.gem

gem update -y



This will update rails to the latest version and install it to your local gem directory.


5) When using a rails application, make sure you add the following to your ./config/environment.rb:

ENV['GEM_PATH'] = '/path/to/your/home/.gems:/usr/lib/ruby/gems/1.8'

 

 

What is the path to Ruby?

 

The path to the Ruby interpretor is:
/usr/bin/ruby

 

 

When I execute the 'Generate' command in my first Rails project, I get numerous errors and the controller is not built.

 

This problem usually appears only after you have created your first application, generated the symlink for your new subdomain, built the database and then attempted to build the controllers for the project according to Knowledgebase article ID: 000232, using the:

[~/rails/first]# ./script/generate controller First list view new edit

command string. The normal result would be several lines of 'created' strings, indicating that the controllers have been successfully built.

Instead, you get several errors beginning with something like:

/usr/lib/ruby/1.8/yaml.rb:133:in `load': syntax error on line 54, col 0: `production:' (ArgumentError)
from /usr/lib/ruby/1.8/yaml.rb:133:in `load'
from /usr/lib/ruby/gems/1.8/gems/rails-1.2.3/lib/initializer.rb:523:in `database_configuration'
from /usr/lib/ruby/gems/1.8/gems/rails- 1.2.3/lib/initializer.rb:228:in `initialize_database'

In other cases, the error strings may begin with something like this:

/usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/connection_adapters/abstract/connection_specification.rb:204:in `establish_connection': development database is not configured(ActiveRecord::AdapterNotSpecified)
from /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/connection_adapters/abstract/connection_specification.rb:195:in `establish_connection'

Both of these instances are indicating that there is a problem, probably structural, with the database.yml file. This file was created by the 'rails first' command and is located in the /rails/first/config/ directory.

The database.yml file is, strictly speaking a 'yaml' file. YAML is "an international collaboration to make a data serialization language which is both human readable and computationally powerful." (follow the link for the citation) Problems occur because of one absolutely inviolable rule with yaml files; they may not use a <TAB> anywhere in the document. Only spaces are allowed. PYTHON users often have problems here, because <TAB> may be used within PYTHON files.
The bottom line is that the database.yml file is very precisely structured and critical to Ruby operations. The combination means that a small error in the database.yml file will cause disastrous consequences in Ruby, usually meaning a complete failure to do what you expected to be done.
Let's take one very obvious instance. The comment in a .yml file is indicated by a '#' at the beginning of the comment. Everything between '#' and the next line is ignored by the interpreter. Some developers habitually use a '#' in their passwords to meet password complexity requirements. A '#' in a password, username, or file name used within a .yml file will cause the interpreter to fail.
Yml files are also strictly hierarchical, similar to an .xml file. What this means is that the following three structures contained within database.yml are not equivalent. The first:

development:
adapter: mysql
database: username_firstdev
host: localhost
username: username_rails
password: password

will work properly, because the arguments of the "Nested Mapping" for "development:" are indented, indicating hierarchy.

This structure:

development:
adapter: mysql
database: username_firstdev
host: localhost
username: username_rails
password: password

will fail because the interpreter will attempt to place "adapter:" at the same level as "development:". In a world where "white space" isn't considered, these would be equivalent. In the Ruby world, they are entirely different constructs.

This structure:


development:
adapter: mysql
database: username_firstdev
host: localhost
username: username_rails
password: password

despite looking similar to the first, will also fail because tabs are used for the argument lines instead of spaces.

Being scrupulous about the structure and syntax of your .yml files in Ruby projects will avoid many of the frustrating problems which can prevent successful deployment of your application. As programmers, we often look only at the text in a file and ignore structure, since, in our world, 'white space' usually has no meaning. In YAML-World, nothing could be further from the truth.

Many thanks to Jarod Reid of the Bluehost Support Team for tracking down the origins of this problem.

 

 

When I attempt to execute my Ruby on Rails application I receive "500- Premature end of script."

 

This error generally has two potential causes:

  • The file permissions are not set to allow the dispatch.cgi to execute properly.

Chmod the dispatch.cgi to 0755.

  • The path to Ruby is is incorrect in the dispatch.cgi file.

The first line of the file is called the shebang-- it sets the location of the intrepretor (in this case ruby).
Change the shebang to the correct path to Ruby (/usr/bin/ruby).
The first line of the dispatch.cgi file should look like this:
#!/usr/bin/ruby