4 What text editor you should use for writting rails applications

On Mac you can use TextMate. You can download it and install for free.

4.3 Gedit

On Linux you can use gedit.

5 How to start Rails application

Ok so you’ve got your tools in order, so we can start to create application.

5.1 Creating new Rails application

Open up terminal window or command prompt if you’re on Windows and enter following code:

$ rails new todo-list-app

This will create rails application named todo-list-app. It takes a while.

5.2 Starting Rails application

Starting Rails application is easy. Just use this command in terminal:

rails server

And look at the rails opening screen.

5.3 Directory structure of Rails app

Our web server is now working, so we can take a look at basic structure of Rails

app/views

Views folder is place for your erb, slim or haml templates.

That’s the file that you will use to install all dependencies what you need
for your applications. They’re called gems. Just add name of gem to your
Gemfile and let Bundler to take care of installation.

app/views

In this folder you place your html templates.

app/assets

Images, Javascript and CSS files belongs here.

5.4 How to use Bundler to install dependencies

After creating rails application you should use Bundler to install all dependencies
what you need. First add name of the gem to your Gemfile like this:

Gemfile

gem'slim-rails'

So here we’ve added slim-rails gem to our gem, which we’ll use later.

Now install it with bundler.

$ bundle install

6 How to use models and controllers to setup database

To save records to database we need to use models

6.1 What are models

Models are special ruby classes that talks to database, store and validates data.

Right now when we know what models are we can create our first model.

rails generate model Todo item:string

This command will create migration create_todos

6.2 What are database migrations

Database Migrations are…

Take look at file with name create_todos.rb

If you want to migrate our table to database.

rake db:migrate

This will create new database with table todos and column item.

If you have sqlitebrowser installed you can take a look at database that we’ve just created. You should see empty database with table todos and columns like this at picture:

6.3 Insert Records to Database

Now we can start to insert records todatabase.

Open up rails console

rails console

Start adding some todos as in the picture.

Certainly you noticed we’ve used Todowith method create I will explain this later in controller section. Check out database in sqlite browser.

7 Using controllers and views to create todos

Ok we’ve our records in database, but we want to show them in browser. To show them we need to create two parts controllers and views.

7.1 What are controllers

Controllers are user request to database. It can be GET request that is like on database is stored form and you want to show form in a browser. And it can be POST request when you fill in a form and send details to database.

Create first controller with name todo

rails generate controller todos

7.2 What are views

Views are html files that user see through browser. We’ll use slim for that. To use rails effectively we need to use ruby code in html templates. Default it is ERB, but slim code is a really shorter. If you have trouble with translating HTML code to Slim, I recommend you html2slim website

7.3 Showing list of all todos

Ok let’s start with creating action that will show all of tasks to browser.
First we need to record all of task in our controller.

app/controllers/todos_controller.rb

defindex@todos=Todo.allend

This command will checkout database for all todos and saves into instance variable todos.

Next thing we need to is to add route

app/views/todos/index.html.slim

- @todos.each do |todo|
ul
li = todo.item

If you wonder why we’ve used folder todos and file index.html.slim inside.So this is basic rails naming convention. If you have controller with name todos, you should have views folder named todos too. And for every GET action(index, new, show, edit) you should have template with name like in controller, this time it is index.

Now we can creates routes.

config/routes.rb

resources:todos

Take look at this at the browser. You should see task we’ve created beforein your web browser.

7.4.1 Adding links to our homepage

app/views/todos/index.html.slim

Typing address to addressbar is really annoying, so to save time we’ll add links to our homepage.