Ruby on Rails
Tutorial

Check out my basic Rails 2.0 application
How to create your first Rails application or click here all of the tutorials

Sample code is worth its weight in gold, so here’s how to get a basic Rails application running.

First, check GettingStartedWithRails or http://api.rubyonrails.org/ for installation and basic setup instructions.

Super-quick “hello world” app

This was written by ReinH as the quickest possible way to get from install to “hello world”.


alias rails_hello_world='rails hello && cd hello && ./script/generate controller welcome hello && echo "Hello World" > app/views/welcome/hello.html.erb && ./script/server -d && firefox 0.0.0.0:3000/welcome/hello'

Requirements

All the requirements are outlined for each operating system at the top of GettingStartedWithRails

Optional

If you’d like a full IDE for rails, try Aptana (formerly “RadRails”)

Otherwise, try TextMate for OS X or E Text Editor as a TextMate clone for Windows.

See Also: Editors for a full list of editors, trick and more.

Getting started

First create a new Rails project using the following command:


rails MyProject

rails-2.0

Rails-2.0.2 changed the default database settings, now it starts new project on sqlite-3. If you still want mysql, you have to type

rails -d mysql MyProject

See: note about rails-2.0.2

Then, start the web server with the following command:


cd MyProject
./script/server

Note: You might need to invoke the Ruby interpreter directly. This is true if you are using InstantRails on Windows – if you are, make sure you are in the command window that you opened through the IR menu. If the above command doesn’t work for you, try this:


ruby ./script/server

Other commands like script/generate will also require that you invoke the ruby interpreter explicitly: ruby script/generate controller whatever.

After running the above command, you should see something like this in the command window:

=> Booting Mongrel (use 'script/server webrick' to force WEBrick)
=> Rails application starting on <a href="http://0.0.0.0:3000">http://0.0.0.0:3000</a>
=> Call with -d to detach
=> Ctrl-C to shutdown server
** Starting Mongrel listening at 0.0.0.0:3000
** Starting Rails with development environment...
** Rails loaded.
** Loading any Rails specific GemPlugins
** Signals ready.  TERM => stop.  USR2 => restart.  INT => stop (no restart).
** Rails signals registered.  HUP => reload (without restart).  It might not work well.
** Mongrel 1.1.2 available at 0.0.0.0:3000
** Use CTRL-C to stop.

Browse to http://localhost:3000 and check that you get the “Congratulations, you’re on Rails!” screen. If you don’t see anything, make sure you’re not running any firewalls.

Or with Apache

It is also possible to use the Apache webserver with Ruby on Rails. To do this follow these steps:

Building a simple application

Note: this tutorial will use WEBrick style URLs. If you’re using Apache please change URLs to match your configuration.

To start, we’ll make a simple “Hello World” type example. For this demonstration, we won’t even use the database.

rails-2.0

The database now gets looked up even if you don’t configure it.

Run the following at the command-line to keep from getting an “Unknown database” error:

rake db:create

To configure Rails not to bother with a database, open Rails’ main config file located in config/environment.rb in your text editor of choice and find the (commented out) line that reads:

  1. config.frameworks -= [ :active_record, :active_resource, :action_mailer ]

Remove the comment and modify the line so that it excludes only ActiveRecord.

config.frameworks -= [ :active_record ]

If you want to keep learning how to develop a super-basic Rails app, continue on to TutorialStepOne.)_

Rails is a MVC (Model View Controller) framework, which means that all the output will happen in the controllers and the views. So the first thing we’ll need is a controller.
Run this from your rails project directory to generate one:


./script/generate controller hello index

This will generate a controller named “hello” with an action “index”.

rails-2.0

Routes are now loaded at start time, so you need to kill your server and bring it back up to pick up the new controller. Setting config.action_view.cache_template_loading = false in
config/environments/production.rb may obviate the need for a server restart.

Try browsing to that action: http://localhost:3000/hello . Since we don’t specify an action after “hello” in the URL, rails assumes the “index” action. We haven’t created a view for this action yet, so Rails doesn’t know what to display:


Hello#index


Find me in app/views/hello/index.html.erb


Now (just like it tells you) open app/views/hello/index.html.erb (index.rhtml for pre-2.0). This is the view for the “index” action in our controller. It contains the text that will be shown at http://localhost:3000/hello (or http://localhost:3000/hello/index ).

URLs in Rails usually follow the format:
hostname/controller/view

Now take a look at app/controllers/hello_controller.rb. This is the controller. It contains program logic that builds the data for the view. Notice the index action. Any public methods in a controller become actions in Rails.

Try adding the following method just before “end” :


def world
  @greeting = "hello world!" 
end

This creates a new action, world that simply creates an instance variable. Note the ”@” sign before the variable name – this lets your views access the variable.

Now browse to: http://localhost:3000/hello/world


Template is missing


Missing template script/../config/../app/views/hello/world.rhtml


Uh oh! We have a problem. The template is missing. “Template” is the Rails word for “View”. And it even tells us the path where it was expecting the template. This path has a lot of ..’s in it, but regardless you can probably see where it’s going. So let’s edit app/views/hello/world.rhtml and add the following text:

rails-2.0

Template naming convention has changed:
world.rhtml is now world.html.erb

See: rails-2 release note


<%= @greeting %>

This grabs the contents of the @greeting variable and outputs it into the template.

Note that these views are RHTML files. You can write regular HTML in them, but you can also write ERB code – embedded Ruby, between < and > tags. But above, we have the strange <%= tag! This tells Ruby to print out whatever the Ruby statements inside tell it to. Try removing the ”=” and reloading the page – you won’t see anything!

Now reload the page. Notice how the controller created a variable @greeting, that carried over into the view.

Next we’ll get started with the model, which will take a little more code. In the meantime, feel free to check out the other tutorials on GettingStartedWithRails.

Go on to TutorialStepOne

A Simple French Tutorial to start with Rails : Ma première application RubyOnRails

Another nice tutorial on Ruby on Rails is : http://www.tutorialspoint.com/ruby-on-rails/index.htm

A good Rails 2.0 tutorial is here .

You can find Ruby on Rails Tutorials in German at Ruby on Rails Blog – Tutorial & Praxiswissen