Ruby on Rails
Tutorial

As a newbie, getting started with Rails was tricky without some help from the IRC folks. If you get stuck, that’s a good place for help, as the author hangs out in there pretty regularly.

That said, some sample code is worth its weight in gold, so here’s how I got a basic Rails application running.

First, check GettingStartedWithRails for some gotchas, and then read http://api.rubyonrails.org/ for some installation tips. I will repeat some of it here, but that is where I started.

Requirements

You can skip a lot of installation problems on Windows by simply using InstantRails

Optional

You can use a IDE to program with Rails.

Get RadRails at http://www.radrails.org

Getting started

1a. Set up Apache for the Rails application (see “Apache configuration example” below1.)
or
1b. Run the WEBrick servlet: ruby script/server -- help (see “WEBrick configuration example” below2.)(Run this from the Rails application directory)
2. Go to http://rails/ (or whatever your ServerName is) and check that you get the “Congratulations, you’re on Rails!” screen
2b. In case of WEBrick, go to http://localhost:3000. Apache2 note: The httpd.conf contains an entry which determines the port to be used. For example, ServerName AServerNameHere:80, port 80 was selected. Try the following url http://localhost:80/.
3. Follow the guidelines on the “Congratulations, you’ve put Ruby on Rails!” screen

Apache configuration example for Apache 2

1 In the Apache configuration below, replace ”/path/application” in each case with the full path to the rails directory unpacked with the tar.gz….

( I am not quite sure what the author means by ” full path to the rails directory unpacked with the tar.gz” In fact it is rather confusing for a ruby newby… I think he means the path that you create by executing “rails /path/to/FILENAME”).

and be sure to append the “public” or “log” directory where noted below. I also had to turn on CGI and set some access requirements – also shown below (Suse 9.1’s Apache is locked down fairly tight)

<VirtualHost *:80>
    ServerName rails
    DocumentRoot /path/application/public/
    ErrorLog /path/application/log/apache.log

    <Directory /path/application/public/>
      Options ExecCGI FollowSymLinks
      AddHandler cgi-script .cgi
      AllowOverride all
      Order allow,deny
      Allow from all
    </Directory>
  </VirtualHost>

An alternative Apache 2 configuration example, that modifies your existing Apache to have a rails enabled directory (rather than a dedicated virtual host) might be

Alias /rails/ /var/www/rails/
<Directory /var/www/rails/>
  Options ExecCGI
  AddHandler cgi-script .cgi
  AllowOverride all
  Order allow,deny
  Allow from all
</Directory>

This alternative configuration will also require

RewriteBase /rails

to be added to the application’s .htaccess file (in /path/to/application/public), where /rails is the value supplied to the app’s Alias directive in httpd.conf.

You should now be able to access your application at:

WEBrick configuration example

2 WEBrick servlet help output

$ ruby script/server --help
Usage: ruby server [options]

    -p, --port=port                  Runs Rails on the specified port.
                                     Default: 3000
    -b, --binding=ip                 Binds Rails to the specified ip.
                                     Default: 127.0.0.1
    -i, --index=controller           Specifies an index controller 
                                     that requests for root will go to 
                                     (instead of congratulations screen).
    -d, --daemon                     Make Rails run as a Daemon (only works if 
                                     fork is available -- meaning on *nix).
    -c, --cache-classes              Caches class compilation which will speed up 
                                     the serving of requests, but require a 
                                     server restart on source changes.

    -h, --help                       Show this help message.

Congratulations, you’re on Rails!

You’ve successfully configured your web-server to point at this Rails application.

Once you have seen this in your web browser, your rails application is ready to begin programming. Follow these steps:

1. TutorialStepOne – Create empty production and test databases for your application.
2. TutorialStepTwo – Edit config/database.yml with your database settings.
3. TutorialStepThree – Create a new controller using the script/generate controller generator
(run with no arguments for documentation).
4. TutorialStepFour – Create a new model using the script/generate model generator
(run with no arguments for documentation).
5. TutorialStepFive – See all the tests run and the app documentation be created by running rake.
6. TutorialStepSix – Develop your Rails application!
7. Setup FastCGI? or mod_ruby to get production-level performance

Resources

category:Tutorial


Translations: