You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

191 lines
7.2 KiB

<html><head>
<title>Building a Basic Ruby on Rails Application</title>
<style>
li {
padding-top: 1em;
}
</style>
</head><body>
<h1>Building a Basic Ruby on Rails Application</h1>
<ul>
<li><b>Part I: Getting Started</b><ul>
<li><a href="#introduction">Introduction</a></li>
<li><a href="#creating_project">Creating a Rails Project</a></li>
<li><a href="#building_model">Building The Data Model</a></li>
<li><a href="#quick_demo">A Quick Demonstration!</a></li>
</ul></li>
<li><b>Part II: Layout and Design</b></li>
<li><b>Part III: Making it Better</b></li>
<li><b>Appendix</b><ul>
<li><a href="#appendix_sdf_utils">Appendix A: SDF Utility Scripts</a></li>
<li><a href="#appendix_database_conf">Appendix B: Configuring A Database</a></li>
</ul></li>
</ul>
<hr />
<h2>Introduction</h2>
<p>[ To Be Written ]</p>
<a name="creating_project" /><h2>Creating a Rails Project</h2>
<p>All rails applications reside in their own directory tree. This tree is
automatically generated by the '<code>rails</code>' command ( '<code>man rails
</code>' for more information ). To create our project's directory structure,
execute the following:</p>
<code>
(alterego@sverige)# rails ~/html/bookmarks --database=sqlite3<br />
(alterego@sverige)# ln -s bookmarks ~/html/rails<br />
(alterego@sverige)# cd ~/html/rails
</code>
<p>The first command creates our project's directory tree under the
'<code>~/html/bookmarks</code>' directory. Then we create a symlink from this
directory, to '<code>~/html/rails</code>' so that the SDF utility scripts will
be able to find, and work with this project. ( <a href="#appendix_sdf_utils">
More info</a> )</p>
<p>The '<code>--database=sqlite3</code>' argument to the '<code>rails</code>'
executable, informs Rails to configure the new project to use an SQLite3
database backend. If you have 'dba' membership then you can use MySQL as your
backend by substituting '<code>sqlite3</code>' with '<code>mysql</code>'. This
tutorial will however focus in using SQLite3 as it is available to all MetaARPA
members. If you are going to use MySQL as your database backend, then you'll
have to read <a href="#appendix_database_conf">this</a> in order to configure
your backend properly.</p>
<p>The final command changes your working directory to your new projects' root.
The rest of the commands in this tutorial rely on you being at this location in
order to execute correctly.</p>
<a name="building_model" /><h2>Building The Data Model</h2>
<p>Now we are sitting in our nice new Rails project's root directory, we can
start building our application. The most important thing in any database driven
application like this one, is the data model. It specifies what data our
application interacts with, and <em>how</em> we interact with it.</p>
<p>In this application, we only have one data type, that is a 'Link', this link
must have a name, some descriptive information and the target URI. Normally at
this stage you'd have to roll-up your sleaves and write one of those fugly SQL
statements to create your table. Not us, Rails has cunningly abstracted database
interaction for us, so no more SQL! In order to create our mode we must run
this command:</p>
<code>
(alterego@sverige)# ruby script/generate model link
</code>
<p>This command line executes the Ruby script, '<code>script/generate</code>'.
The '<code>generate</code>' script is extremely useful, it automates the process
of creating files to add specific functionality to our applications. You can
tell what operations a '<code>generate</code>' command has performed by reading
it's output. This command created the two files required to create our 'Link'
model.</p>
<p>Next we need to edit the '<code>db/migrate/001_create_links.rb</code>' file.
Files located under the '<code>db/migrate</code>' directory are used to perform
revision changes on your project's database. This file will allow us to specify
our databases table without the need for any SQL, and, if required, roll our
database back to before this migration occured.</p>
<h3>Edit '<code>db/migrate/001_create_links.rb</code>'</h3>
<pre>
class CreateLinks < ActiveRecord::Migration
def self.up
create_table :links do |t|
t.column :name, :string
t.column :info, :string
t.column :url, :string
end
end
def self.down
drop_table :links
end
end
</pre>
<p>Now we've specified our '<code>links</code>' table's structure. We have to
commit the revision in order to create the table. To do this execute the
following:</p>
<code>
(alterego@sverige)# rake db:migrate
</code>
<p>Going into the details of this command is way outside the scope of this
document, I wouldn't have used it in this tutorial if it wasn't for it being
such an easy way to generate a database across different backends.</p>
<a name="quick_demo" />
<h2>A Quick Demonstration!</h2>
<p>Before we move on to defining the application controller, I thought I would
try and impress you with Rails magic. The next two commands will allow you to
list, view, edit and delete items from your database through your web browser:
</p>
<code>
(alterego@sverige)# ruby script/generate scaffold link<br />
(alterego@sverige)# ruby script/server -p `id -u`
</code>
<p>Right, those two commands did quite a bit. The first one generated a basic
set of HTML templates and application logic. The second command started a
web server, written in Ruby, running on a port that was specified from your
userid. You'll notice on the second line of output from the '<code>server
</code>' script, the address that it has bound itself too, along with the port
number ( your user id ) it is running on in standard URI format.</p>
<p>So, open up a web browser, and point it to
'<code>http://sverige.freeshell.org:[ YOUR USER ID ]</code>' substituting your
user id. Your browser should open 'Ruby on Rails: Welcome aboard' page. This
just indicates that the Rails environment and server are running. Now, go back
to your browsers' address bar, and append '<code>/links</code>' after your user
id. Your browser should now show a rather rubbish looking list view, with no
elements.</p>
<p>At this point it will probably be a good idea to play around and add some
items as we'll need them for the next sections. Besides, I need to make myself a
drink ...</p>
<h2 style="text-align: center;">To Be Continued ...</h2>
<hr />
<a name="appendix_sdf_utils" /><h2>Appendix A: SDF Utility Scripts</h2>
<p>There are two utility scripts written specifically for Rails applications
on SDF. The first 'ror' toggles whether the Rails project under '<code>
~/html/rails</code>' has it's server started when the syste boots. The second
script 'railsctl', is start/stop daemon, which starts or stops the Rails
project located in the standard SDF project location.</p>
<p>As you may want to play with multiple Rails projects, it doesn't really
matter where you put them, or what you call them. But if you plan on using the SDF utility scripts, which is a good idea if you want to host your project,
then it's probably a good idea to symlink your current project directory to
'<code>~/html/rails</code>'</p>
<a name="appendix_database_conf" /><h2>Appendix B: Configuring A Database</h2>
<p>[ To Be Written ]</p>
<div style="font-size: 8pt;">
$Id: Rails_Basic_Application.html,v 1.2 2006/12/31 14:27:54 alterego Exp $
</div></body></html>