Quantcast
Channel: Andrew den Hertog » Ruby On RailsAndrew den Hertog
Viewing all articles
Browse latest Browse all 3

Beginner Rails – Creating a new application

$
0
0

It’s quick to get up and running in ROR – more so than any other web stack. This example takes on a photo blog application to show the basic commands on getting a site up and running.

First, declare a new project using the command:

rails new photoblog –d mysql

 

The –d mysql option tells rails to create a new project preconfigured to use mysql. It’s your choice what you use, and the complete set of choices are:

mysql/oracle/postgresql/sqlite3/frontbase/ibm_db

 

This generates the skeleton files needed for a rails app:

The next step is to define our database connections. Open up config/database.yml:

If you’re using a different provider, your configuration file will be different. Check over the defaults and make any necessary changes.

Once your connections have been defined, we need to create them. Rather than doing it ourselves, we’ll get rake to do this for us.

rake db:create:all

When this finishes, you’ll notice that rake has created the databases for all environments:

So that’s the basic setup and configuration of a rails app. The next step is to create the entities for our application. In this photoblog app, we’ll create 3 entities:

We’ll have a number of galleries, that each contain some photos, and each photo can have comments attached against it.

 In development it’s easiest to create entities in order of their dependence. That is, create objects that don’t depend on anything else and work backwards from there. In this case, the entities will be created:

  1. Gallery
  2. Photo
  3. Comment

 

We’ll use a process in rails called scaffolding to get the Gallery set up. Scaffolding is a quick and dirty method to get a basic set of MVC objects together to interact with an entity.

To do this for the Gallery entity, use:

rails generate scaffold Gallery title:string

2 things about this – firstly we didn’t explicitly create an id field (this is handled implicitly for all rails objects), secondly rails automatically pluralizes all of your entity names.

At this point all of these files exist, but the database doesn’t have the “galleries” table created. This is a convention of rails where models are created in a migration script (under db/migrate), and need to be applied to your database using the command:

rake db:migrate

If you open these migration scripts, you’ll notice there’s no SQL. This makes it dead easy to switch to different database providers later on without having to retrofit all of your code.

At this point you can check out your basic gallery page by booting up your web server. To get a list of all of the links in your site, use:

rake routes

By default, RoR comes bundled with a WEBrick server, so to start it up all you need to do is:

rails server

 

The server starts up on: http://127.0.0.1:3000, and a visit to one of the routes (eg: http://127.0.0.1:3000/galleries) can be seen in the console:

As well as your browser:


Pretty basic, unstyled site. The next post will have a deeper look into model relationships, and how to gain more control over your application development without using scaffolding.


Viewing all articles
Browse latest Browse all 3

Trending Articles