Ruby on Rails/Getting Started/Creating the application
First Application
[edit | edit source]Create a basic Rails Application
[edit | edit source]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
[edit | edit source]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:
File/Folder | What is it good for |
---|---|
app/ | This is the folder where you will work most of the time. It contains your model, view and the controller. |
app/model | Contains all your models for your project. |
app/view | Includes all HTML files inside a folder. These files are named after an corresponding action inside your controller. Aditionally, these files are grouped into folders with the controller name. |
app/controller | Holds the logic for your Rails application. |
db/ | All database related files go into this folder. If you are working with SQLite, then the database is likely to be in this folder as *.sqlite3 file. |
db/migrate | Has all the migrations that you created. |
config/ | As the names suggest, it has all the necessary configuration files of your Rails application. There is only very little configuration needed, so no worries that you have to crawl through endless lines of code. Localizations, routes and all basic files can be found here. |
public/ | All your static files (files that do not change dynamically) your CSS or JavaScript files are inside this folder. |
public/images | All your static Images |
public/javascripts | When you embed Javascript, CSS or images, the default location is inside these folders. Rails will automatically look inside these to find the proper file. |
public/stylesheets | When you embed CSS, the default location is inside this folder. Rails will automatically look inside this to find the proper file. |
script/ | Holds scripts for Rails that provide a variety of tasks. These scripts link to another file where the "real" files are. Inside these folder you will find the generators, server and console. |
log/ | Log files from your application. If you want to debug some parts of your application you can check out these files. |
test/ | All files for testing your application. |
doc/ | Documentation for the current Rails application. |
lib/ | Extended modules for your application. |
vendor/ | Whenever you have 3rd-party plug ins, they will be found in this folder. |
tmp/ | Temporary files |
README | Basic guide for others on how to setup your application, what specialities it has or what to be careful about. |
Rakefile | Handles all the Rake tasks inside your application. |
Basic creation of different files
[edit | edit source]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 (20090420180053_create_tags.rb) 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.