There was a problem loading the comments.

User Guide - Ruby on Rails

Support Portal  »  Knowledgebase  »  Viewing Article

  Print

Applicable Plans: All eApps General VPS Plans

User Guide – Ruby on Rails

Ruby on Rails, often called RoR or just Rails, is an open source web application framework written in Ruby that closely follows the Model-View-Controller (MVC) architecture. It strives for simplicity and allowing real-world applications to be developed in less code than other frameworks and with a minimum of configuration. The Ruby programming language allows for extensive meta programming, which Rails makes much use of. This results in a syntax that many of its users find to be very readable.
-from Wikipedia, http://en.wikipedia.org/wiki/Ruby_on_Rails

Overview

The eApps Hosting service is designed to make it easy for you to use Ruby on Rails. The purpose of this document is to walk you through the setup of a Ruby on Rails application in your eApps VPS and provide information on advanced techniques that you can use to improve performance and reliability.

Currently there are a number of excellent Rails programming tutorials and sources for Ruby on Rails information available online. This guide is not intended to be a reference source, but rather provides you with the necessary configuration steps that are specific to your eApps hosting environment.


This User Guide explains the basic setup of a Ruby on Rails application on the eApps Hosting service. If you follow the guidelines, you will remain safely within the eApps Support envelope. If you choose a custom configuration you may be subject to service fees if our assistance is needed to help you resolve issues that you may encounter.

Requirements

Ruby on Rails – two example applications

Configuring and deploying your application

Using RubyGems

Links to other information

Requirements

At a minimum you will need to have the Ruby on Rails application service installed in your eApps Virtual Private Server (VPS). Ruby on Rails is available in all plans offered by eApps. You can select Ruby on Rails during the order process, or later from the System tab of your control panel.

In most cases you will also need a database server. If you need database access for your application, please make sure that one of the supported database servers is installed and available in the All Applications area on the System tab.

To install Ruby on Rails and a database server, follow these steps:
Login to your Control Panel, and click on the System Tab. If necessary, click the Select Another System (Subscription) link on the left and choose the correct Virtuozzo system.

Go to Applications, and click on the Add Application link. Select the box next Ruby-On-Rails, and also the database server you wish to install, and then scroll down and click the Next button. If you do not see Ruby-On-Rails or the database server in the Add Application list, click on the All Applications link and see if they are already installed.

This takes you back to the All Applications screen. Wait for five minutes, then click on the Refresh link at the upper right, just under the word Parallels. The applications should now show as installed. If it is still in a Scheduled state, wait another five minutes, and click Refresh again. If you see it in Error state, or it still shows as Scheduled, please contact eApps Technical Support.

If you choose to deploy your Ruby on Rails application using mod_rails for Apache, you will also need to install the mod_rails application.

The mod_rails application is only available in the CentOS 5 plans. If you do not see mod_rails in either the Add Application list or the All Applications list, you may not have it available to install. To see what operating system (OS) you have, click on the Subscriptions icon from the My Account tab of your Control Panel. Then click on the name of the subscription you want to see. The OS for the subscription will be displayed near the top of the page. If you are not on a CentOS 5 plan, but would like information on updating your plan, please contact eApps Technical Support for more information.


Ruby on Rails – two example applications

Here are two simple examples to demonstrate how Rails works, and to help you get started with your own application. Both examples use the obligatory "Hello World" application, often a programmer's first step in working with a new language.

We recommend that you do this first example to verify that Ruby on Rails has been installed in your VPS service properly and that you understand how to get a basic Ruby on Rails application up and running.

The second example details a more complicated setup that communicates with a MySQL database backed to generate a scaffolding.

Note The examples that follow assume that you are able to login to the VPS using SSH, and are comfortable working from the command line using standard Linux commands, and can edit files in the vi text editor. You cannot use the Control Panel to set up Ruby on Rails applications in the eApps Hosting service.


The following examples use the webadmin user and directory and example.com as the placeholder domain. Please substitute your user and domain name on your VPS. Run the commands as the user who owns the directory where you are creating the rails application. 

Example 1 - Hello World in Ruby on Rails

[webadmin@example]$ cd /home/webadmin/example.com/html
[webadmin@example html]$ rails hello
      create
      create  app/controllers
      create  app/helpers
      create  app/models
....(more output)
[webadmin@example html]$
[webadmin@example html]$ cd hello/
[webadmin@example hello]$ ruby script/generate controller hello
      exists  app/controllers/
      exists  app/helpers/
.....(more output)
[webadmin@example hello]$
[webadmin@example hello]$ cd app/controllers/
[webadmin@example controllers]$ vi hello_controller.rb

Edit the hello_controller.rb file, changing it from:

class HelloController < ApplicationController
end

to:

class HelloController < ApplicationController
def index
render :text => "hello world"
end
end

and save and exit the file.

[webadmin@example controllers]$ cd ../../config/
[webadmin@example config]$ pwd
/home/webadmin/example.com/html/hello/config
[webadmin@example config]$ vi routes.rb

Edit the routes.rb file, changing the end of the file from:

map.connect ':controller/:action/:id'
map.connect ':controller/:action/:id.:format'
end

to:

map.connect ':controller/:action/:id'
map.connect ':controller/:action/:id.:format'
map.connect '', :controller => "hello"
end

In the line to be added the ‘’ are two single quotes, with no space between them. The symbols around "hello" are regular double quotes.
Save and exit the file.

In the same directory, edit the environment.rb file, and uncomment the line that allows you to use this example without a database.

[webadmin@example config]$ vi environment.rb

Uncomment this line:

# config.frameworks -= [ :active_record, :active_resource, :action_mailer ]

and save and exit the file. Remember to comment this line back when building your app with a database!

[webadmin@example config]$ cd initializers/
[webadmin@example initializers]$ vi new_rails_defaults.rb

Comment out this line:

ActiveRecord::Base.store_full_sti_class = true

and save and exit the file.  Remember to uncomment this line when building your app with a database!

Move the index.html file out of the way in the public/ directory:

[webadmin@example initializers]$ cd ../../public/
[webadmin@example public]$ pwd
/home/webadmin/example.com/html/hello/public
[webadmin@example public]$ mv index.html{,.bck}

Now start the Ruby server to serve the example application with the steps below.

[webadmin@example public]$ cd ..
[webadmin@example hello]$ pwd
/home/webadmin/example.com/html/hello
[webadmin@example hello]$ ruby script/server webrick -p 8000
=> Booting WEBrick
=> Rails 2.3.4 application starting on http://0.0.0.0:8000
=> Call with -d to detach
=> Ctrl-C to shutdown server
[2009-10-13 13:33:11] INFO  WEBrick 1.3.1
[2009-10-13 13:33:11] INFO  ruby 1.8.6 (2008-08-11) [i386-linux]
[2009-10-13 13:33:11] INFO  WEBrick::HTTPServer#start: pid=23759 port=8000

Navigate to the site on port 8000 to see the Rails app running. For this example, the URL would be http://example.com:8000

Hello World!
Hello World screen shot

When you’re ready stop the web server, use Cntrl+c (hold down the Control key, press the c key).

[2009-10-13 13:59:30] INFO  going to shutdown ...
[2009-10-13 13:59:30] INFO  WEBrick::HTTPServer#start done.
Exiting
[webadmin@example hello]$
 

Example 2 - Hello World using MySQL and scaffolding in Ruby on Rails

Note A database and database user must be created before starting this example

To create the database and database user, login to your Control Panel, and click on the System Tab. If necessary, click the Select Another System (Subscription) link on the left and choose the correct Virtuozzo system.

Go to Databases Management, and then click on Add Database. Enter the Database Name (in this example ror_test) and click Save.  In Add Database User, enter the Database Username (in this example ror_user) and a password. Make note of the name of the database, the database user, and the password.

Remember that everything from the command line has to be done as the user who owns the Rails application. The following example uses webadmin as the user and example.com as the placeholder domain. Please substitute your actual user and domain as necessary.

[webadmin@example] $ cd /home/webadmin/example.com/html
[webadmin@example html]$ rails --database mysql helloWorld
create
create app/controllers
create app/helpers
create app/models
~
~
create log/server.log
create log/production.log
create log/development.log
create log/test.log
[webadmin@example html]$
[webadmin@example html]$ cd helloWorld/config/
[webadmin@example config]$ vi database.yml

Edit the database.yml file, and change the settings for the development database. All the other settings in test and production can be left as is.

Change the file from:


~
~
development:
adapter: mysql
encoding: utf8
reconnect: false
database: helloWorld_test
pool: 5
username: root
password:
socket: /var/lib/mysql/mysql.sock

to:

~
~
development:
adapter: mysql
encoding: utf8
reconnect: false
database: ror_test
pool: 5
username: ror_user
password: passwd
socket: /var/lib/mysql/mysql.sock

and save and exit the file.

[webadmin@example config]$ cd ..
[webadmin@example helloWorld]$ pwd
/home/webadmin/example.com/html/helloWorld
[webadmin@example helloWorld]$ ruby script/server
=> Booting Mongrel
=> Rails 2.3.4 application starting on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server

This starts the Ruby on Rails application server, and the default Welcome to Rails web page is displayed at http://example.com:3000

Ruby on Rails default page
Welcome aboard Rails

This shows that Ruby on Rails is working as expected. The next step is to create a controller for the helloworld application.

[webadmin@example helloWorld]$ ruby script/generate controller say helloworld
      exists  app/controllers/
      exists  app/helpers/
      create  app/views/say
      exists  test/functional/
      create  test/unit/helpers/
      create  app/controllers/say_controller.rb
      create  test/functional/say_controller_test.rb
      create  app/helpers/say_helper.rb
      create  test/unit/helpers/say_helper_test.rb
      create  app/views/say/helloworld.html.erb
[webadmin@example helloWorld]$
[webadmin@example helloWorld]$ cd app/controllers/
[webadmin@example controllers]$ vi say_controller.rb

Edit the say_controller.rb file, and change the file  from:

class SayController < ApplicationController
  def helloworld
  end

end

to:

class SayController < ApplicationController
  def helloworld
    @HelloWorld = "Hello world"
  end
end

and save and exit the file.

[webadmin@example controllers]$ cd ../views/say/
[webadmin@example views]$ pwd
/home/webadmin/example.com/html/helloWorld/app/views/say/
[webadmin@example say]$ vi helloworld.html.erb

Edit the helloworld.html.erb file, and change the file from:

<h1>Say#helloworld</h1>
<p>Find me in app/views/say/helloworld.html.erb</p>

to:

<h1><%= @HelloWorld %></h1>

and save and exit the file.

If necessary, stop the web server with CTRL+c and restart it with the ruby script/server command. The new page is displayed at http://example.com:3000/say/helloworld

Hello World!
Hello World

The next step is to be able to read and write to the MySQL database from Ruby on Rails using scaffolding.

[webadmin@example helloWorld]$ ruby script/generate scaffold greeting message:string
      exists  app/models/
      exists  app/controllers/
      exists  app/helpers/
~
~
create    test/fixtures/greetings.yml
      create    db/migrate
      create    db/migrate/20091021144316_create_greetings.rb
[webadmin@example helloWorld]$

Next, generate the database table. If you receive any errors here, go back and check the database.yml file to make sure it is correct.

[webadmin@example helloWorld]$ rake db:migrate
(in /home/webadmin/example.com/html/helloWorld)
==  CreateGreetings: migrating ================================================
-- create_table(:greetings)
   -> 0.0032s
==  CreateGreetings: migrated (0.0033s) =======================================

[webadmin@example helloWorld]$

Restart the web server with CTRL+c and ruby script/server. The new page is displayed at http://example.com:3000/greetings

Greetings
Listing Greetings

Click on New greeting, and enter the new greeting.

New Greeting
New greeting

As the last part of the example, change the SayController to show the last message as the greeting.

[webadmin@example helloWorld]$ cd app/controllers/
[webadmin@example controllers]$ vi say_controller.rb

Edit the say_controller.rb file, and change the file  from:

class SayController < ApplicationController
  def helloworld
    @HelloWorld = "Hello world"
  end
end

to:

class SayController < ApplicationController
  def helloworld
        @HelloWorld = Greeting.find(:last).message
  end

end

and save and exit the file.

 Restart the web server with CNTRL+c and ruby script/server.  The new page, showing the last greeting entered is displayed at http://example.com:3000/say/helloworld



New Greeting!!

These two short examples show the very basics of how to create a simple Ruby on Rails application, and how to set up an application to connect to a MySQL database. For more advanced configurations, please refer to the official Ruby on Rails documentation or one of the many online tutorials and web sites available for Ruby on Rails.


Configuring and deploying your application

There are two methods we recommend for deploying your Ruby on Rails application, using either mod_rails or Mongrel. Please review each deployment method to ensure that you have selected to best approach for your application.

Note mod_rails is the recommended approach for deploying your Ruby on Rails applications on the eApps service

If you wish to use either Mongrel or mod_rails, please see the User Guides for those approaches. Note that both of those User Guides assume you have read though this User Guide, and worked through the examples if necessary.
If you need assistance on how to upload your already created Ruby on Rails applications to the VPS, please see the FTP or SFTP User Guides.

Using RubyGems

You may want to add functionality to your application using RubyGems. A gem is a packaged Ruby application or library. A great introduction to RubyGems can be found at http://www.rubygems.org/.

RubyGems keep you from having to reinvent the wheel, by providing you with modular pieces of code that can be accessed from within your application's code.


Warning
Ruby Gems will also allow you to update the Ruby on Rails application service itself. DO NOT DO THIS!!

If you install an updated version of Ruby on Rails or any component of the application service using RubyGems, you will be outside of the eApps Support guidelines, and any assistance with Ruby on Rails will become billable at $15 per 10 minute increment.

Always upgrade your Ruby on Rails application service from the Upgrade Applications area on the System tab of your Control panel. This will ensure that you are running on an eApps supported version.

By default, some gems will come pre-installed with your hosting plan. Generally these are Rails-relevant gems (ActiveRecord, ActionMailer, etc.). To see a list of installed gems at any time, type "gem list --local" from an SSH window.

[webadmin@example ~]$ gem list --local

*** LOCAL GEMS ***

actionmailer (2.3.4)
actionpack (2.3.4)
actionwebservice (1.2.6)
activerecord (2.3.4)
activeresource (2.3.4)
activesupport (2.3.4)
cgi_multipart_eof_fix (2.5.0)
daemons (1.0.10)
fastthread (1.0.6)
gem_plugin (0.2.3)
mongrel (1.1.5)
rack (1.0.0)
rails (2.3.4)
rake (0.8.4)
(List accurate as of October 2009, please check manually to get the current list and versions)

To see a list of all the available gems on rubyforge.org, use the --remote flag. Currently there are 5404 gems on Rubyforge, so pipe the list into a file for easier reading. This example uses gem.txt as the output file, but you can use any new file you wish. Just don't use an existing file, or the contents of that file will be overwritten by the list of gems.

[webadmin@example ~]$ gem list --remote > gem.txt
[webadmin@example ~]$ cat gem.txt | wc -l
5404
[webadmin@example ~]$ more gem.txt
aalib-ruby (0.7.1)
aargvark (0.0.15)
abn (1.3.0)
AbsoluteRenamer (1.0.4, 1.0.3, 1.0.2, 1.0.1)
abstract (1.0.0)
~
~
~
zsff (1.0.0)

zvent (0.0.3)
zyps (0.7.6)
[webadmin@example ~]$

Installing Gems

Gems have to be installed as the root user, so that the files for the gems are in the default system path. If you do not have root access to the VPS, you will need to contact the VPS owner and have them either install the gems for you, or give you root access. If you are not an authorized contact, eApps Support will not assist you with installing gems, and will refer you to the VPS owner for more assistance.

The command to install gems is gem install gem_name - where gem_name is the name of the gem you wish to install. This command will attempt to download the gem from RubyForge. More installation options can be found with gem install --help


You can update gems individually with gem update gem_name and you can update the RubyGems application itself with gem update --system

This example installed the gem yoda:

[root@example ~]# gem install yoda
Successfully installed yoda-1.0.0
1 gem installed
Installing ri documentation for yoda-1.0.0...
Installing RDoc documentation for yoda-1.0.0...
[root@example ~]# gem list --local

*** LOCAL GEMS ***

actionmailer (2.3.4)
actionpack (2.3.4)
actionwebservice (1.2.6)
activerecord (2.3.4)
activeresource (2.3.4)
activesupport (2.3.4)
cgi_multipart_eof_fix (2.5.0)
daemons (1.0.10)
fastthread (1.0.6)
gem_plugin (0.2.3)
mongrel (1.1.5)
mongrel_cluster (1.0.5)
rack (1.0.0)
rails (2.3.4)
rake (0.8.4)
yoda (1.0.0)  < -- here is the new gem
[root@example ~]#

To view the documentation on the installed gems, you will need to use the RubyGems Documentation Index, which can be found at http://example.com:8808/  
To start the RubyGems Documentation server, use the gem server command as the root user.

[root@example ~]# gem server
Starting gem server on http://localhost:8808/

(you will need to use Cntrl + c to stop the gem server)

Installing Gems in small plans

The Gems install process consumes a lot of memory. If you are running the Standard plan, or any plan with low memory availability, you may have problems running Gems. If so, please use this alternative procedure to manually install the Gem you require:
  1. Install the gem on your local machine using the standard gem install and find the .gem file OR download the .gem file from the remote repository to your local machine
  2. Upload this .gem file from your local machine onto your eApps server
  3. From the directory you uploaded the .gem file, perform a gem install gem_name.gem

RubyGems errors

Many errors with RubyGems (especially related to upgrading RubyGems) can be resolved by deleting the gem cache. For example, errors that look like ERROR: While executing gem ... (NoMethodError) can be resolved by running the command rm -f /usr/lib/ruby/gems/1.8/source_cache. Below is an example:

[root@example ~]# gem update yoda
Updating RubyGems...
ERROR: While executing gem ... (NoMethodError)
undefined method efresh' for #<Hash:0xb794cd1c>
[root@example ~]# rm -f /usr/lib/ruby/gems/1.8/source_cache
[root@example ~]# gem update yoda
Bulk updating Gem source index for: http://gems.rubyforge.org
Successfully installed yoda-1.0.0


Links to other information

Ruby on Rails Wiki - http://wiki.rubyonrails.org/
The Ruby programing language (Ruby is the actual programming language, Rails is a framework written in Ruby) - 
http://www.ruby-lang.org/
Ruby on Rails main site, with links to the official Ruby on Rails documentation - http://rubyonrails.com/
RubyGems, with links to documentation and downloads - http://www.rubygems.org
Open Source Ruby projects - http://rubyforge.org/

Share via
Did you find this article useful?  

Related Articles


On-Premise Help Desk Software by SupportPal

Categories

© Cloudscale365