Ruby is an amazing programming language and Ruby On Rails is based on it, so you'll need to learn how to program in Ruby to create your Apps using Ruby On Rails.
What is Ruby on Rails?
Ruby on Rails is a framework that makes the development, deployment, and maintenance of a web application easier and it uses an object-oriented language known as Ruby. To introduce it, the developer needs to know some of its philosophies. These are:
DRY-Don't Repeat Yourself: means that while development in Rails has the same part of the code or the same purpose in different entities, this means that there is a better way to write your application;
Convention over Configuration: means that instead of determining the configuration, Rails has structural and named conventions that implement the often-cited principle of least surprise (POLS);
Less Software: means using more conventions, fewer codes, fewer complexities, and, consequently, lesser amounts of bugs.
A framework is a collection of libraries and tools capable of facilitating development. A good framework provides a basic, but complete, infrastructure for building an application. These facts make Rails one of the best. After all, it has an independent platform, complete and licensed for free (open source).
The development of Rails applications is implemented using MODEL-VIEW-CONTROLLER, better known as MVC architecture, and worked with Active Record, Action View, and Action Controller libraries. These will be cited below in the article.
The MVC Model
This architectural pattern used divides the application logically into three categories: model, view, and control (Model-View-Controller). Each part of the MVC standard is an entity capable of being built and tested separately. The model represents the data, the view represents the user interface and the control commands the actions, that is, the model is the information that the application works with, the view is the representation and the control (controller) is the director of the interaction between them.
The cycle of this structure begins when the user interacts with the interface (view) and calls an event; the controller receives the call from the view and accesses the model, frequently updating the interface.
Ruby on Rails libraries
In Ruby on Rails, each library has a specialized task. When combined, these individual entities map directly from the MVC standard:
Active Record: A library responsible for database abstraction and interaction;
Action View: A template system that generates the HTML documents that the user receives as a result of a request to the application;
Action Controller: A library to manipulate both: the flow and the bank data to be displayed in the view.
The Ruby on Rails language
Many other languages require you to write the code and copy it to check the results and errors. However, Ruby is a consistent and elegantly object-oriented programming language and the best of all is dynamics, which means that you work with the "live" language, that is, you will receive feedback on each command given.
The interactive interpreter is IRB in Terminal (if using Linux or Mac) or Cmd (if using Windows). To start a session, type:
Ruby has a package called RubyGems responsible for managing the system with the function of installing libraries and utilities very important for Rails. As of version 2.0.0, it is not necessary to download separately because it is already included.
How to install Ruby on Rails
To develop in Rails on your computer it will take three steps:
Install SQLite, which is the default database.
You will need some tools, such as git when installing packages and some gems that are libraries of reusable Ruby files.
Installing Ruby on Rails on Mac OS
To install Ruby on Rails on MAC (in this article we will use version X 10.8 Mountain Lion), you must have on your machine:
XCode: This is available on the App Store, free of charge, and can be your source for compiling your Ruby code.
Homebrew: is a package created for OS X that is written in Ruby. This will help you install other parts. To install enter the Terminal and enter the following code:
ruby <(curl -fsSkL raw.github.com/mxcl/homebrew/go)
RVM: Now you need to install a common Ruby tool called Ruby Version Manager (RVM). This will help you manage Ruby versions and various gems that you may use in your projects. As with Homebrew, you only need one command in Terminal:
curl -L https://get.rvm.io | bash -s stable
If using Mavericks and the code above does not work, run the following code on the terminal:
ruby -e "$(curl -fsSL https://raw.github.com/mxcl/homebrew/go/install)"
Ruby: The Ruby language is pre-installed on your Mac. The 2.0.0 package includes RubyGem. To check, run the following code in the Terminal:
If an older version of the language is presented, then we will update to version 2.0.0. Use the following code:
rvm install 2.0.0
After the installation is complete, we need to tell RVM which version of Ruby we want to use. For this you will need to enter the following code:
rvm use 2.0.0
To test whether your machine is working with such a number use the following code:
What will return in the terminal is: ruby 2.0.0p481 and other data that depends on updates and installation times.
Rails: Finally, we will install the framework that needs only one command:
gem install rails
Ready! You already have Ruby, Rails, and SQLite on your computer.
How to install Ruby on Rails on Windows
To install Ruby on Rails on Windows, we will need to follow the procedures below:
Ruby: On Windows, we started with the language. To install you will need to enter the website.
Depending on your machine, you will click on Ruby 2.0.0-p481 or Ruby 2.0.0-p481 (x64). After downloading the installer, you will click on the icon, continue, and then choose where you want the installation folder to be and what you would like to install together with the Ruby package.
Td/Tk support: provides Ruby with a graphical user interface (GUI = Graphical User Interface);
Add Ruby executables to your PATH: ruby is added as an executable;
Associate .rb and .rbw with this Ruby installation: files with this format will be associated with ruby.
It is recommended that the last two be marked. Td/Tk support can then be installed and is used for more advanced applications. After finishing, enter the command prompt (Start > cmd ) and enter the following code:
The answer should be ruby 2.0.0p481 and other data that depends on updates and installation times.
Rails: Since Ruby 2.0 has RubyGems, we just update with the command:
gem update -system
To install Rails, the gem command may take a while, so type in cmd again:
gem install rails
To check: rails -v
SQLite: Enter the SQLite website: and download two files: SQLite and SQLite DLL. Always opt for the most updated available. These files will be in .zip format. Unzip the two files and place them inside the C:\Ruby20\ bin folder or where you placed it. To find out if it was installed correctly, enter cmd and enter the following command:
The answer will be the version of SQLite you downloaded. Finally, we still need to install the Ruby library, which allows contact between it and SQLite. To do this, write the following code at the prompt:
gem install sqlite3-ruby
Ready! You have everything installed to start an application on your Windows machine.
Installation on Linux
Linux (and UNIX-based systems in general) has several features, but also a lot in common. These instructions will be based on Ubuntu Linux but should be applied to most UNIX systems.
Before you start, you need to update your Ubuntu. Open the terminal and write the following code:
$Sudo apt-get update
The apt-get program has a cache index of all programs and their versions in a repository for faster searching. Running the update command ensures that this list will be the most recent possible.
In addition, we need to install the items below:
Curl: It is a library that supports questions involving URLs. To make it easier we will use it. Just enter the following code in Terminal:
$Sudo apt-get install curl
Ruby: Before installing Ruby, some libraries are needed for the components being installed. Enter with the command:
$Sudo apt-get install build-essential curl
Now, as on the Mac, we will use Ruby Version Manager (RVM), in case there are other versions of Ruby installed on the machine. Enter the following code:
$ curl –L https://get.rvm.io | bash –s stable
To make sure that Ruby is already on your machine, type the command:
Rails: Now RubyGems is already installed. Then, put this command:
$gem install rails
After some time, to check Rails and its version use the code: rails --version
SQLite: With apt-get you only need one command:
$Sudo apt-get install sqlite3 libsqlite-dev
If everything goes smoothly, you can check the version of the installation with: $SQLite –-version Now to create a relationship between Ruby and SQLite we will need a Ruby library that allows this contact. Use the following command:
$sudo gem install sqlite3-ruby
Ready! You can now start exploring and creating Rails applications on Linux Ubuntu.
Creating Your First Ruby On Rails Application
From now on, regardless of what your operating system is, you can use and follow this article on your machine.
To create projects you need to use the terminal, where you will give commands followed by the word rails inside the directory you want. In this part of the article, we will introduce what any beginner needs to do as their first application: a “Hello World”.
Remember that you do not need to create a folder for the project because when using the new command, a folder with the name of the application will be created automatically. Then open the Terminal, enter the directory you want, and type:
$rails new hello
After this command, several files will be created inside a newly generated hello directory. The terminal will show all created folders and some of them are:
Notice that a subdirectory called app has been created. In it are the views, models, and controllers of the MVC standard that had their concepts introduced here.
The helpers are part of libraries and will help the "relationship" between views and controllers. In the app will also be the mailers that were not mentioned before, but will be part of the application's email questions.
Now we will place the mini project on the local web server so that it can be tested. No code was written, however, Rails has a splash screen for you. But first, make sure it is inside your application's directory in Terminal and that your gems are up to date. To do this, use the following code:
When your gems are finished reviewing and installed, write the code:
When initializing, the IP address 0.0.0.0 appears on port 3000. Do not be alarmed by these zeros. They just mean that the server is running locally on your machine while it runs in Terminal. Then enter any browser and access the URL http://www.localhost: 3000/ and you will see the Ruby on Rails Hello World page
Generating a Controller in Ruby on Rails
First, stop the local server in Terminal by pressing Ctrl + C which will leave it in the project folder. Now, we will create a controller to send the message Hello World.
The rails generate command creates certain files for your project. You are dealing with a request and a response cycle to create certain files, so you will create a controller responsible for greeting like this:
$rails generate controller salutation
The controller and files related to it, such as views, test, and helper, will be created. You could create by hand, but generate speeds up production.
Now, to show “Hello World” you will need to modify the app/controllers/salutationcontroller.rb file by creating an action. Open this file in your editor and make the following changes:
class SalutationController < ApplicationController
@message = ‘Hello World!’
Actions are implemented as methods in Ruby, where def is the key. Within the method, you created there is an instantiated variable called message with the value that we will put in the browser.
Generating a Template in Ruby on Rails
The action was created successfully, so the next step is to add some HTML to be shown on the web. With the MVC standard, which separates the files, this code will be in the views.
The default is to always create the view with the name of the action in the controller. Then, when accessing app/views, you will see that the salutation directory has already been created, but without files. Open a new tab in your editor, name it hello.html.erb, and save it within app/views/salutation.
You may be wondering why the format at the end of an HTML file is .erb. This format is part of the Erb Templating library, a Rails library responsible for allowing the use of Ruby code within HTML code.
The Ruby syntax is introduced with <% =%> when it will show a value in HTML, or <%%> when it will only have logic, known as Embedded Ruby output tags.
Enter the app / views / salutation / hello.html.erb file and add the following sequence:
<h1><%= @message %></h1>
Now you must tell Rails how to respond to the URL and the person responsible will be routes.rb located inside the config directory. At the moment do not worry too much about details, as it is only the beginning and you still lack information that will be given little by little. Open config/routes.rb in the text editor and replace what is written (there is no need to delete the comments, these have # before the sentences):
You are asking for the message to be shown in the browser at the URL http: // localhost: 3000 / salutation / hello . Upon opening, your message “Hello World!” will be showing up.
Authentication in ASP.NET MVC:
To implement an authentication mechanism in ASP.NET MVC applications. We will create here a project that will include user registration, login, logout, and password change, essential features for any application that has areas of restricted access to logged in users.
Access control in Laravel with ACL:
To implement an authorization system using Laravel, the most widely used PHP framework in the world. For that, we will start with an application that has all the user registration and authentication parts.
ASP.NET MVC and Identity: Authorization of users with claims: To implement an access control mechanism using ASP.NET Identity, which allows us to assign roles to users and from that define which pages and menus one can view.
More about Ruby
Architectural patterns in Ruby:
This demonstrates the use of ArchRuby to express architectural patterns and good practices that, once preserved, contribute to the maintainability of the software system.
Refactorings in Ruby: Using the Extract Class technique:
Know the Extract Class refactoring technique and learn how to use it in the Ruby language to improve the quality of your code.
Arrays and Blocks with Ruby using the IRB:
To develop in Ruby using Arrays and Blocks. Arrays have several methods that help to facilitate the work of developers, while blocks are among the great news brought by Ruby.
Creating a simple application with Ruby On Rails:
Building a simple application to demonstrate interesting features provided by the Ruby On Rails framework