Posted on 3rd November 20092 Responses
Quick and Dirty Guide to your 1st Rails app – Part #1

For all of you who want to take a peek at creating a Ruby on and need a quick start, this post is designed to aid you in getting to know RoR. I will post follow-ups to this to make this first better and better and to incorporate more complex functionality in a step-by-step manner.
Ok, so you want to quickly get started with your first in Ruby on ? Here we go…

First, install rubygems via your favourite package manager or head over to www.rubyonrails.org, grab the package and follow the instructions. Then install some gems you need (I assume you like mySQL so I install the mysql gem for DB connection but you can of course also go with postgreSQL or sqlite).

Notice: this guide is written for Linux!

Make sure you have the development libs of your favourite database (mySQL, …) installed or else you will run into trouble when installing the gems.

sudo gem install mysql railroad

This will install , the mysql connector, the make tool (which is needed for several tasks from migrating to stuff to your database, inserting prepared data into it or updating your javascripts and much more).

Now you’re set to pick a folder (I tend to make a / folder in my home dir) for your apps:

mkdir /home/<myusername>/

and create your first app:

cd /home/<myusername>//
-d mysql myFirstApp
cd myFirstApp

You will notice that has created some folder for you. The ones that concern us at the moment are:

app/
config/
db/
public/
script/
vendor/

In app/ all the necessary controllers, models and views as well as partials live (we’ll get to that later), config/ contains configuration files and environment-specific extra configuration as well as the database schema file, db/ contains the database migrations we need to spice up our database, public/ contains stuff like javascripts, images, stylesheets etc. and vendor/ is the place where we can put our gems and plugins.

An especially important directory is script/ which contains our generators (which provide us with means of creating functionality, models etc.) and the launcher for ’ built-in webserver.

Important hint: DO NOT delete these folders unless you know what you are doing – it WILL break your app!

I know this is a bit hefty at the moment, but trust me – it will become clear once we work with the files and folders (I will introduce some of the concepts of Model-View-Controller (MVC), the concepts and basic ideas and so on throughout this ).

Let’s jump right into the action and take a look at our database config. Start by editing config/database.yml :

After a clean install, it will look like this:


development:
adapter: mysql
encoding: utf8
reconnect: false
database: myFirstApp_development
pool: 5
username: root
password:
socket: /var/run/mysqld/mysqld.sock


# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "".
# Do not set this db to the same as development or production.
test:
adapter: mysql
encoding: utf8
reconnect: false
database: myFirstApp_test
pool: 5
username: root
password:
socket: /var/run/mysqld/mysqld.sock


production:
adapter: mysql
encoding: utf8
reconnect: false
database: myFirstApp_production
pool: 5
username: root
password:
socket: /var/run/mysqld/mysqld.sock

Since I take it you know how to create a database and how mySQL works – just fill in the appropriate data in the fields and save it. You may change the database names but I strongly recommend you to to keep the syntax of dbname_test, dbname_development and so on.

Done? Great! Now let’s tell to create our databases:

db:create:all

If you take a look at your mySQL server (e.g. via phpMyAdmin) you see that has created the databases for us. You may now fire up the server:

ruby script/server

and head on to http://localhost:3000 to see a RoR placeholder there. So far so good. Now remove the index.html page from public/ since it is just the placeholder page and reload your browser window – ooops! An error has occured. Get used to these nifty error messages from RoR – you will see many of them, trust me ;)

But no hassle – we will fix this immediately. The cause of this error is that we have killed the placeholder but our app doesn’t know where to go now. So we have to tell it the right way to a controller (which we don’t have yet) so myFirstApp can display something. For this, we have to create a controller:

ruby script/generate controller welcome

Now you will see that creates some files:

exists  app/controllers/
exists  app/helpers/
create  app/views/welcome
exists  test/functional/
create  test/unit/helpers/
create  app/controllers/welcome_controller.rb
create  test/functional/welcome_controller_test.rb
create  app/helpers/welcome_helper.rb
create  test/unit/helpers/welcome_helper_test.rb

exists means that has found out that the directory already exists so it won’t be created, created means that has created a file or folder.

In app/, has created a controller (where our programming goes), a model (which basically is our database table), a view folder (where we can put HTML files to display what the controller pulls from the model) and a helpers folder. We will discuss these later, just let’s stay straight and take a look at our controller:

class WelcomeController < ApplicationController
end

Not much here, eh? So let’s put an index action there which controls what is displayed when one calls http://localhost:3000/welcome/ in the browser:

def index
@data = "Hello World..."
end

Notice: If you have not taken a look at Ruby and its reference – this will definitely a good time to do so or else you won’t be capable to code stuff for your !

Ok, what is this? @data is a variable which contains the string “Hello World…”. This variable has been defined inside the index function of our controller which means that in our index view, we can use it. But – guess what? Right, we have no index view yet. So let’s create it. That’s simple: just put a file named view.html.erb in our app/views/welcome/ folder. There, just put one line:

<%= @data %>

What the heck is this? Tataa… it’s ruby code! <%= basically is the same as <?php echo or <?php print - it puts out the data our variable is holding to the browser. So when you call our brand new index view via

http://localhost:3000/welcome/

you will see that “Hello World…” is displayed. For now, we want that our welcome controller view is called whenever someone navigates to the root of our by entering http://localhost:3000 – we can achieve this via ’ routing configuration in config/routes.rb:

When you open this file in your editor, you notice a lot of stuff that’s already there. At the moment, only the line

# map.root :controller => "welcome"

is interesting. Simply remove the # to uncomment it, save the file and restart the server (CTRL+C in the shell and then start it up again). That’s it – welcome is now our main controller! And that’s it for our first lesson – you have created your first !

Share this on:
  • Print
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Add to favorites
  • email
  • MisterWong
  • MySpace
  • Reddit
  • RSS
  • Yahoo! Bookmarks

Related posts

Comments
comment by t.morgan
Posted on 05.11.2009 at 18:39

I’m looking forward to a follow up to this post – maybe you can write something more extensive about ActiveScaffold usage?

comment by Stefan Schuchlenz
Posted on 05.11.2009 at 18:51

I’m thinking of doing some kind of workshop in the future, but at first I have to finish my basic Rails HOWTO series. Thanks for the idea though.

Leave a Response
XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <img src="">