292 lines
10 KiB

<title>Ruby on Rails on SDF</title>
<h1>Ruby on Rails on SDF</h1>
<a href="#whatisrails">What is Rails?</a></li>
<li><a href="#prerequisites">Prerequisites</a></li>
<li><a href="#starting">Getting started</a></li>
<li><a href="#hello">Hello, world!</a></li>
<li><a href="#maintenance">Maintenance and Utility Scripts</a></li>
<h2><a id="whatisrails" />What is Rails?</h2>
<a href="http://www.rubyonrails.org/">Ruby on Rails</a> is a WebDev framework written in the
<a href="http://www.ruby-lang.org/">Ruby</a> programming language.
<h2><a id="prerequisites" />Prerequisites</h2>
You must be a MetaARPA member to use Rails (see <a href="http://sdf.lonestar.org/index.cgi?access">memberships</a> page).
Most Rails applications use a database, so you'll probably want dba membership as well (see
<a href="http://sdf.lonestar.org/index.cgi?access">memberships</a> page). However MetaARPA
members may use sqlite if they so desire.
<h2 id="starting">Getting started</h2>
<b>Step 1:</b> Run <code>mkhomepg</code> if you have not already done so
(see the
<a href="http://sdf.lonestar.org/index.cgi?tutorials/building_a_website" >building_a_website</a>
tutorial for more information on the <code>mkhomepg</code> command).
<b>Step 2:</b> 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
<p>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.</p>
<b>step 3:</b>
<p>Start the webrick server on the port that is equal to your uid, which you can obtain by executing 'id -u':</p>
$ 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
<p>Then point your browser at <code>http://yourdomain:&lt;YOUR UID&gt;</code> You should now be looking at the default 'Welcome to Rails page'. Now you can begin to develop your rails app.</p>
<h2><a id="hello" />Hello, world of Rails!</h2>
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
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
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 URL
<code>http://yourdomain:60844/say/hello</code> 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.
&lt;html&gt;&lt;head&gt;&lt;title&gt;Hello, Rails!&lt;/title&gt;&lt;/head&gt; &lt;body&gt;
&lt;h1&gt;Hello from Rails and SDF!&lt;/h1&gt; &lt;/body&gt; &lt;/html&gt;
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.
<h2><a id="maintenance">Maintenance and Utility Scripts</h2>
There are currently two main scripts that MetaARPA members can run to manage
their RoR instance. The first is <i>ror</i> which is used to toggle automatic
startup of your RoR instance upon system boot. The second is <i>railsctl</i>
which allows you to <i>start</i>, <i>stop</i> and <i>restart</i> your RoR
<hr /><h3>Footnotes:</h3>
<a name="finding_uid"></a><a href="#uid"><sup>1</sup></a>Use the <code>id</code> command to find your
$ 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.
<font size=1><cite>$Id: Ruby_on_Rails.html,v 1.7 2006/12/29 06:54:24 phm Exp $</cite></font>