Thursday, September 6, 2007

Basic 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 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.rhtml && ./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”)
Getting started

First build a rails project using the following command:


rails MyProject

Then, start the WeBRICK webserver with the following command:


cd MyProject
./script/server

If you’re on Windows, the script/server isn’t marked executable, or the #! line is incorrect for your system for some other reason, you may have to use


ruby ./script/server


and other similar commands may also require that you explicitly invoke the ruby interpretter.

Then browse to http://localhost:3000 and 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 block localhost, port 3000.
Or with Apache

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

* Set up Apache for the Rails application (see GettingStartedWithRails)
* Go to http://rails/ (or whatever your ServerName is) and check that you get the “Congratulations, you’re on Rails!” screen.
_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/

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 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 all the files necesary for our example. Try browsing to http://localhost:3000/hello . And you should see something like:

Hello#index

Find me in app/views/hello/index.rhtml

Now (just like it tells you) open app/views/hello/index.rhtml. This is the View. It contains the text that will be shown at http://localhost:3000/hello (or http://localhost:3000/hello/index ).

Change it however you want. Or leave it alone.

So it should be getting clearer now that 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 function. Any public methods on a controller become actions in Rails.

Try adding the following method just before “end” :


def world
@greeting = "hello world!"
end

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 a 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:


<%= @greeting %>

Now refresh that last page.

hello world!

Well look at that. Notice how the controller created a variable @greeting that carried over into the view. All the instance variables (ones that start with @) from the controller get pulled into the view.

Next we’ll get the Model involved, which will take a little more code. In the mean time, 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

A Simple Turkish Tutorial to start with Rails : GMYT

No comments:

About Me

Ordinary People that spend much time in the box
Powered By Blogger