Ruby on Rails on SDF


What is Rails?

Ruby on Rails is a WebDev framework written in the Ruby programming language.


  • You must be a MetaARPA member to use Rails (see memberships page).
  • Most Rails applications use a database, so you'll probably want dba membership as well (see memberships page). However MetaARPA members may use sqlite if they so desire.

Getting started

Step 1: Run mkhomepg if you have not already done so (see the building_a_website tutorial for more information on the “mkhomepg” command).

Step 2: Use the rails command to construct the base for your new rails application:

  $ cd $HOME/html
  $ rails rails
        create  app/controllers
        create  app/helpers
        create  app/models
        create  app/views/layouts
        create  config/environments
        create  components
        create  db
        create  doc
        create  lib
        create  lib/tasks
        create  log
        create  public/images
        create  public/javascripts
        create  public/stylesheets
        create  script/performance
        create  script/process
        create  test/fixtures
        create  test/functional
        create  test/integration
        create  test/mocks/development
        create  test/mocks/test
        create  test/unit
        create  vendor
        create  vendor/plugins
        create  tmp/sessions
        create  tmp/sockets
        create  tmp/cache
        create  Rakefile
        create  README
        create  app/controllers/application.rb
        create  app/helpers/application_helper.rb
        create  test/test_helper.rb
        create  config/database.yml
        create  config/routes.rb
        create  public/.htaccess
        create  config/boot.rb
        create  config/environment.rb
        create  config/environments/production.rb
        create  config/environments/development.rb
        create  config/environments/test.rb
        create  script/about
        create  script/breakpointer
        create  script/console
        create  script/destroy
        create  script/generate
        create  script/performance/benchmarker
        create  script/performance/profiler
        create  script/process/reaper
        create  script/process/spawner
        create  script/runner
        create  script/server
        create  script/plugin
        create  public/dispatch.rb
        create  public/dispatch.cgi
        create  public/dispatch.fcgi
        create  public/404.html
        create  public/500.html
        create  public/index.html
        create  public/favicon.ico
        create  public/robots.txt
        create  public/images/rails.png
        create  public/javascripts/prototype.js
        create  public/javascripts/effects.js
        create  public/javascripts/dragdrop.js
        create  public/javascripts/controls.js
        create  public/javascripts/application.js
        create  doc/README_FOR_APP
        create  log/server.log
        create  log/production.log
        create  log/development.log
        create  log/test.log

It has become SDF policy to have the project that you want to be served under '$HOME/html/rails' in actuality you can locate the rails project directory anywhere and call it what you like. The 'railsctl' and 'ror' command expect the project that you want to host to be located in the afore-mentioned directory. So, if you want your server to be started when the host starts and 'railsctl' to find your project then you should symlink your current project to that location, or use that location.

step 3:

Start the webrick server on the port that is equal to your uid, which you can obtain by executing 'id -u':

  $ railsctl start
  - or -
  $ ruby $HOME/html/rails/my_first_project/script/server -p `id -u`
  => Booting WEBrick...
  => Rails application started on
  => Ctrl-C to shutdown server; call with --help for options

Then point your browser at “http://yourdomain:<YOUR UID>” You should now be looking at the default 'Welcome to Rails page'. Now you can begin to develop your rails app.

Hello, world of Rails!

Rails is a Model-View-Controller framework. Rails accepts incoming requests from a browser, decodes the request to a controller, and calls an action method in that controller. The controller then invokes a particular view to display the results to the user. The good news is that Rails takes care of most of the internal plumbing that links all these actions. To write our simple Hello, World! application, we need code for a controller and a view. We don't need code for a model, because we're not dealing with any data. Let's start with the controller.

In the same way that we used the rails command to create a new Rails application, we can also use a generator script to create a new controller for our project. This command is called generate, and it lives in the script subdirectory of the my_first_project project we created. So, to create a controller called Say, we make sure we're in the my_first_project directory and run the script, passing in the name of the controller we want to create.

  $ ruby $HOME/html/rails/my_first_project/script/generate controller Say
  exists app/controllers/
  exists app/helpers/
  create app/views/say
  exists test/functional/
  create app/controllers/say_controller.rb
  create test/functional/say_controller_test.rb
  create app/helpers/say_helper.rb

The script logs the files and directories it examines, noting when it adds new Ruby scripts or directories to your application. For now, we're interested in one of these scripts and (in a minute) the new directory. The source file we'll be looking at is the controller. You'll find it in the file app/controllers/say_controller.rb. Let's have a look at it.

  class SayController < ApplicationController

Pretty minimal, eh? SayController is an empty class that inherits from ApplicationController, so it automatically gets all the default controller behavior. Let's spice it up. We need to add some code to have our controller handle the incoming request. What does this code have to do? For now, it'll do nothing. We simply need an empty action method.

Let's add an action called hello to our say controller. Adding a hello action means creating a method called hello in the class SayController. But what should it do? For now, it doesn't have to do anything. Remember that a controller's job is to set up things so that the view knows what to display. In our first application, there's nothing to set up, so an empty action will work fine. Use your favorite editor to change the file say_controller.rb in the app/controllers directory, adding the hello method as shown.

  class SayController < ApplicationController
     def hello

Now let's try calling it. Navigate to the URLhttp://yourdomain:60844/say/hello” in a browser window. (Note that in the development environment we don't have any application string at the front of the path - we route directly to the controller.) You'll see something that looks like an error.

It might be annoying, but the error is perfectly reasonable (apart from the weird path). We created the controller class and the action method, but we haven't told Rails what to display. And that's where the views come in. Remember when we ran the script to create the new controller? The command added three files and a new directory to our application. That directory contains the template files for the controller's views. In our case, we created a controller named say, so the views will be in the directory app/views/say. To complete our Hello, World! application, let's create a template. By default, Rails looks for templates in a file with the same name as the action it's handling. In our case, that means we need to create a file called hello.rhtml in the directory app/views/say. (Why .rhtml? We'll explain in a minute.) For now, let's just put some basic HTML in there.

<html><head><title>Hello, Rails!</title></head> <body> 
<h1>Hello from Rails and SDF!</h1> </body> </html>

Save the file hello.rhtml, and refresh your browser window. You should see it display our friendly greeting. Notice that we didn't have to restart the application to see the update. During development, Rails automatically integrates changes into the running application as you save files.

Maintenance and Utility Scripts

There are currently two main scripts that MetaARPA members can run to manage their RoR instance. The first is ror which is used to toggle automatic startup of your RoR instance upon system boot. The second is railsctl which allows you to start, stop and restart your RoR instance.


<sup>1</sup>Use the “id” command to find your uid

$ id
uid=60844(phm) gid=500(arpa) groups=500(arpa),600(MetaARPA)

Note: if you do not see metaARPA here you're not in the metaARPA group and Rails won't work for you. $Id: Ruby_on_Rails.html,v 1.7 2006/12/29 06:54:24 phm Exp $