Ruby on Rails is a very effective Ruby framework, which uses MVC pattern and does not require compilation phase. This article contains installation instructions and the list of top 5 of the most needed Ruby sample code examples for beginners. We have to mention, that you are required to know basics of Ruby and understand principles of OOP in order to understand these sample code examples. And, of course, you should be an advanced internet user and be familiar with web development in general. Otherwise we recommend you to hire us for creating apps of any difficulty.
Installation process
The process of installation on Windows is easy. It is expected, that you have the newest Ruby version installed. And, of course, during the process the Interactive Ruby Shell will be handy.
Type the following line in Ruby to install Rails:
C:\>gem install rails
You can use the framework on Windows now!
Rails Installation on UNIX like systems (Linux, Ubuntu) requires the “rbenv” program and JS Runtime Environment (Node.js). After installation of Ruby via rbenv, use this command (replace *.*.* with the numbers of current Rails version):
tp>install rails -v *.*.*
After that we need to type the next command in order to make Rails executable:
tp>rbenv rehash
Sample 1: Simple and quick creation of empty web app
Before writing your own code, you’ll have to learn basics. In our first ruby on rails sample we’ll make an empty application. First of all, open the ruby installation directory. Let’s now type the following line to build a skeleton for your application:
tp>rails new library
Now you have a subdirectory, which contains full directory tree for an app.
Sample 2: Create a server for your app
In this sample we will create our own server and check our empty library app – type the following line in order to do that:
tp>cd ruby\library
tp\ruby\library\>rails server
Congratulations! You’ve just run a default rails web server.
Try to use your browser to check address: http://localhost:3000. You are going to see the following message:
Sample 3: Connect the framework to your database
Here is a ruby on rails sample code (a part of file database.yml) that connects Rails to your database (of course, you can choose your own preferences):
development:
adapter: example_db_adapter
database: example_db
host: localhost
username: user
password: pass
You can change this code next time you need to use a different database.
Sample 4: Making Active Record Models
Rails Active Record is the ORM (Object Relational Mapping), that comes with the framework. It gives you binding and interface for tables from a database and program code. Let’s use two simple Active Record models:
Books;
BookShelve (contains groups of books).
To make the Active Record files, execute this command:
library\>rails generate model book
library\>rails generate model book_shelve
Besides many other files, Rails then will generate the next directories with default code sceleton:
- Model file, which will contain unique methods and business rules for each model;
- File “fixtures”;
- Unit test file;
- Files book.rb and book_shelve.rb (they will contain a default code skeleton).
Sample 5: Making associations for our Models
You can use these types of relationships between your Models:
- 1-to-many;
- many-to-many;
- 1-to-1.
We have to let the framework know, what kind of association you need to have in your data system. Make changes in book.rb as in the following ruby on rails code example:
class Book < ActiveRecord::Base
belongs_to :book_shelve
end
Now change book_shelve.rb:
class BookShelve < ActiveRecord::Base
has_many:books
end
Yes, making associations is that simple!
If you would like to learn more about Rails web app and to see more example of code contact us.