| Home | Business | Personal | Contact |
||
Max S. Dunn...when there is a will, there is a way |
||
Let’s create our first Rails app.
(In the previous section, there were separate boxes to show what to type and what the output looks like. Now that we are all at least sophmore Rails programmers, we will dispense with that convention and just show what you type interspersed with the output.)
Rails will create an empty application for us:
C:\>mkdir \rails
C:\>cd \rails
C:\rails>rails demo
create
create app/controllers
create app/helpers
create app/models
...
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
C:\rails>
This won’t do anything, but sets up the files and directories we need.
Now let’s create a simple “Hello from Rails” application. Type:
C:\rails>cd \rails\demo
C:\rails\demo>ruby 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
C:\rails\demo>
Now edit the app\controllers\say_controller.rb file to look like this:
class SayController < ApplicationController
def hello
end
end
And create app\views\say\hello.rhtml:
<html>
<head>
<title>Hello Rails</title>
</head>
<body>
<h1>Hello from Rails!</h1>
</body>
</html>
Now start the Webrick server:
C:\rails\demo>ruby script\server
=> Booting WEBrick...
=> Rails application started on http://0.0.0.0:3000
=> Ctrl-C to shutdown server; call with --help for options
[2006-07-11 21:13:31] INFO WEBrick 1.3.1
[2006-07-11 21:13:31] INFO ruby 1.8.4 (2005-12-24) [i386-mswin32]
[2006-07-11 21:13:31] INFO WEBrick::HTTPServer#start: pid=2840 port=3000
And open your new Rails application:
http://localhost:3000/say/hello
Impressed? I am!
— – –
(This example taken from the fantastic Rails book Agile Web Development with Rails . Get a few copies so you can give some to your friends!)
Let’s take a few minutes to look at the directory structure of this Rails application. Remember, that Rails emphasizes “Convention Over Configuration” so all Rails apps you will run into will have this same structure.
The app directory is where all your code goes. Inside are four directories: controllers, models, views and helpers. In controllers there is one file for every controller; in the other directories, there will be a subdirectory corresponding to each controller.
The config directory is where, umm, the config files go. Besides the app directory, this is the only other directory that you will need to mess with to get your Rails application running.
The db directory is where you keep your database schema files and the migration files. Migrations are a great way to keep the database up-to-date and in sync with the Rails code, no matter what database changes you make.
The public directory is the only one that the web server sees. It contains the dispatch.cgi file (which is how the Rails app gets started) and also files served up directly to the web server. So if you need to put images on your Rails pages, or edit the stylesheet, this is where you do it.
The other directories are pretty much self explanatory, and we won’t need them for this tutorial