Ruby on Rails/Getting Started/Creating the application

Create a basic Rails Application
Creating your first Rails application is as simple as typing: rails firstapplication in the console. This will create the Rails application in the current directory. If you need to use another database than the default (SQLite3) you can specify the database with the -d parameter. If you need e.g MySQL support, create your app with rails firstapplication -d mysql

This sets up the basic files and folders you need. Let's have a look at the files and folders and what they are good for:

The Application Structure
After creating your Rails application, you will be presented with a directory structure. The following table should introduce you to the folders and their purpose:

Basic creation of different files
Most of Rails files can be created via the console by entering a simple command that tells Rails what you want. This way you can create database migrations, controllers, views and much more. All commands look like rails generate generator generator-options

To see what options are available, from your applications directory just enter cd firstapplication rails generate and Rails will show you all available options and what generators are currently installed. By default, you can choose from the different generators. The most important are:


 * controller
 * helper
 * mailer
 * migration
 * model
 * scaffold

If you want more information on different generators, simply enter the generator command e.g. script/generate model in the console and you will get information for this specific command and examples explaining what the generator does.

When working with generators, Rails will create and name all the necessary files in a proper way according to the conventions. This is especially important when working with Migrations (see later). Rails will never overwrite your files if they already exist (unless you tell Rails explicitly to do so).

A very good way to get you started with everything you need is to scaffold your files. This will create a basic CRUD-structure for your files. (CRUD=Create Read Update and Delete; this reflects SQL attributes create, insert, update and delete) Scaffolding creates not only your migrations but also a controller with the most basic syntax and a view-folder, that comes with templates for very basic displaying and editing of your data. To use scaffolding, we will use our generators. The scaffolding generator expects the name of a MODEL/CONTROLLER and the proper fields and types (in the format field:type).

Say, we want to create tags for different products, we can use: rails generate scaffold Tag name:string popularity:integer

Inside the console, Rails will give us information about the files it created: exists app/models/ exists app/controllers/ exists app/helpers/ create app/views/tags exists app/views/layouts/ exists test/functional/ exists test/unit/ exists test/unit/helpers/ exists public/stylesheets/ create app/views/tags/index.html.erb create app/views/tags/show.html.erb create app/views/tags/new.html.erb create app/views/tags/edit.html.erb create app/views/layouts/tags.html.erb identical public/stylesheets/scaffold.css create app/controllers/tags_controller.rb     create  test/functional/tags_controller_test.rb     create  app/helpers/tags_helper.rb     create  test/unit/helpers/tags_helper_test.rb     route  map.resources :tags dependency model exists   app/models/ exists   test/unit/ exists   test/fixtures/ create   app/models/tag.rb     create    test/unit/tag_test.rb     create    test/fixtures/tags.yml exists   db/migrate create   db/migrate/20090420180053_create_tags.rb

Let's take a quick tour of the files we have now: First Rails created a separate folder inside the view named tags. Inside this folder we have some different templatefiles. Besides, we have a controller (tags_controller.rb), files for tests (tags_controller_test.rb, tags_helper_test.rb, tags.yml, tag_test.rb), helpers (tags_helper.rb), a migration file (<tt>20090420180053_create_tags.rb</tt>) and last but not least, a route was also added. As you can see, scaffold does quite a lot of work for us. But remember, that the generated code is very basic and far from being "good" but it already gives you a good starting point.