Ruby on Rails Tutorial

Expedite your Ruby on rails skill set with our recently designed HKR’s Ruby on rails tutorial. In this Ruby on rails tutorial, you will be learning the introduction, revolution, features, architecture, and directory structure. Ruby on rails is a type of extremely productive web application framework which is written in Ruby programming language by David Heinemeier Hansson. This tutorial will provide you a better understanding of the Ruby language. Ruby on rails tutorial is specially designed for those who would like to use the Ruby framework for developing the database packed web framework application. What you are waiting for? Let’s start learning this popular framework:

Introduction to Ruby on Rails:

Ruby is a dynamic, reflective, object-oriented, general-purpose programming language. Ruby is a popular programming web application framework developed by Ruby developers. We can also say that Ruby on Rails is a server-side web application framework written in Ruby programming language. This Ruby language developed under the MIT university language. A rail is also a model view controller framework application that offers a default database structure, a web page, and a web service. The main motto of developing this Rail framework is to offer abstract oriented and also simplifies the common repetitive tasks. The current version of Ruby which we are using now is 6.0.3.2 (June 17th, 2020).

Why Ruby on Rails is so popular?

The following are the important factors that define why Ruby is so popular:

1. With the help of ruby on rails framework, the process of programming is much faster than when compared to another language. This is because of its object-oriented framework, vast data collection, and open-source code facility.

2. The Ruby on rails convention also makes it developers easy to develop codes, and also very flexible to move different rails projects when they consist of the same structure and coding practices.

3. Ruby on rails is a good choice for developing rapid applications as a framework and also easy to perform any changes.

4. Ruby code is a readable and self-documenting tool and increases productivity. This language increases the productivity level, as it requires less code is written and separate documentations.

5. This rail is mainly developed to support the testing team; this is because it offers good testing framework applications.

6. Most of the rails libraries are open source, when compare to other commercial development frameworks and doesn’t require any licensing cost.

Ruby on rails 6.0.3.2 version features:

Below are the major features offered by the Ruby on rails framework:

1. Action mailbox:

Rails 6.0 now ships with inbuilt support for mail guns, mand drills, postmark and send grills. The email use processed via active record and stored on the cloud through active storage and can be sent to multiple mailboxes.

2. Action Text:

Most WYSIWYG, editors are wrappers around HTML, APIs, and reverse engineer from there. Action text (Trix) includes active storage backed file, image processing, and a text-processing flow that ties it all together.

3. Multiple database support:

Rails 6 has launched support for multiple primary databases, which is very useful for applications seeking to scale horizontally rather just vertically or using NoSQL and SQL databases in tandem.

4. Run test cases in Parallel:

To enable efficient usage of multiple codes (either on your machine or a CI server). Rails 6 introduced the option to spin multiple processes and threads to execute your test suites in parallel leading to faster deployments.

5. More love for JAVA scripts:

Web packer is now the default javascript bundler for rails through the new apps. The javascript directory although CSS and static assets will continue to work through the asset pipeline.

6. ZeitWerk:

Zeitwerk is the new code loader for rails, built from scratch. It enables hot reloads of code not just within rails but also from the gems as well.

Ruby on Rails architecture:

The following diagram explains the work nature and component details of Ruby on rails architecture design:

IMAGE

Ruby on rails uses the Model View Controller or MVC architectural pattern. This model also follows the convention over multiple configuration rules this is also known as coding by convention. The main purpose of Ruby on rails architecture is to increase the performance of the application.

Let me explain the components of Ruby on rails architecture:

Model: this is the lower level pattern used for maintaining the data structure.

View: this type of component is used to display the data or any portion of the data mainly used by users.

Controller: this component consists of software codes. These codes are often used to control the interactions between the model and view components.

As we already know that Ruby on rails is a popular tool and this isolates the application logic from the available user interface layer and also supports the separation of software concerns. The main function of the controller is to receive all the requests for the application developments and then it works with a model that prepares any data which are needed by the view component. The view then makes use of these data structures prepared by the controller component to create final presentable responses. The view is a script-based templates system available in the following programming languages like JSP, ASP, PHP, and also it is very easy to integrate with AJAX technologies.

Ruby on Rails framework:

A framework is nothing but a simple program, a set of different programs, and a code library. The code library in the Ruby framework helps you to write any programming applications. The following three major tasks need to be performed during the time of framework development application.

Describe and model any application domain:

The domain is known as a popular universal application, and the domain can be of any type such as music stores, universities, address books, dating services, and a hardware inventory. The main task is to figure out what content in it, what type of entities exist in the universe, and how these items are related to each other. This task is equivalent to modeling a database structure to maintain the entities and their relationship.

Specify what can happen in the domain:

The domain model is static, and here you have to make it dynamic. Here addresses are added to an address book and musical scores will be purchased from music stores. Users can log into any dating service. Students can register for university classes. All you need to do is identify all the possible scenarios or any actions that the element can participate in a domain.

Ruby on Rails Certification Training

  • Master Your Craft
  • Lifetime LMS & Faculty Access
  • 24/7 online expert support
  • Real-world & Project Based Learning
Choose and design the publicly available views of the domain:

This is the final task, here you can think about web browser terms. Once you have decided that, your domain name is a student and they register for classes in universities. These domain details can be stored in form of a welcome page, confirmation page, and registration page. Each of these pages, views, shows how things stand at a certain point.

Ruby on Rails MVC framework:

This model view controller framework divides the work of any application into three different cooperative subsystems.

Ruby on Rails MVC framework

1. Modeler (active record method):

This model feature maintains the relationship between the object and any database applications. It also helps to handle validations, associations, transactions, and more. The model subsystem is implemented in the ActiveRecord class library, this offers an interface and binding between any two tables in a relational database management system, and the ruby program codes also are executed.

2. View (Action view method):

This is a type of presentation of data method in a particular format, which is triggered by a controller decision mainly used to present the data. These data are available in the form of script-based templates such as JSP, ASP, Ajax, and PHP.

3. Controller (Action controller):

This type of method directs the data traffic, querying the data models for specific data tasks, and also organizes the data sets (like searching, messaging, and sorting) into a form that should fit into the given view.

Directory representation of MVC framework:

Below is the standard directory syntax:

tp > cd/usr/local/lib/ruby/gems/2.2.0/gems

tp > ls

The three MVC frameworks on Ruby rails can be represented as:

Actionpack-x.y.z

ActiveRecord-x.y.z

Rails-x.y.z

Over a windows installation, you can see them like this:

tp > cd ruby\lib\ruby\2.2.0\gems

ruby \lib\ruby\gems\2.2.0\gems\>dir

Directory representation of MVC framework

Ruby on rails –Directory structure:

When you use the rails helper script helps to create an application, and this creates the entire data structure for the given application.

Now it’s time to know the purpose of each directory:

1. app: this directory helps to organize the application component. This consists of various subdirectories that hold the components like view or (views and helpers), the controller (controller method), and the backend level business logic (a type of model).

2. app/controllers: the controller subdirectory where ruby on rails used to find the controller classes. A controller in rails handles web-based application requests from the user.

3. app/helpers: The helpers’ subdirectory consists of helper classes that are used to assist the MVC or model, view, and controller classes. This helper subdirectory keep the model, controller, and view code can be focused.

4. app/models: this model subdirectory consists of classes that help the models to wrap the data stores in the application database. In most frameworks, the part of the application can grow messy, verbose, tedious, and error-prone. Ruby on rails makes it simple.

5. app/view: this view subdirectory contains the display templates to fill in the data from any application, convert them to HTML, and return these data to the user’s browser.

6. app/view/layout: this subdirectory holds the template files for layouts that will be used with views. This model helps the common header/footer method to wrap up the views.

7. Components: this component directory contains tiny self-contained applications, views, controller, and bundle models.

8. Config: this config subdirectory consists of a small number of configuration codes. There are n –number configurations available such as database configuration (in database.yml), daily environment structure (environment. rb), and routing of incoming web requests.

9. db: we already know that the rails application will have the model object that helps to access relational database tables. With the help of db you can also script the database models and create a place in the directory to store these data.

10. lib: here you will put class libraries here, namely vendor libraries.

11. log: in this subdirectory, you can store the error log files. Ruby on rails creates the script to manage various error logs related to relational database models. Here you will find the server logs such as development.log files, test.log files, and production.log files.

12. Public: public directories are like web servers, and these web server files never change. Here you can see a lot of public related files such as JavaScript files (public/JavaScript), graphics (public/images), stylesheets (public/stylesheets), and HTML files (Public files).

13. Script: this subdirectory file holds the scripts which are used to launch and manage various rails tools. For example there are scripts that are used to generate the codes (generate) and launch the webserver (server applications).

14. Test: This test subdirectory helps you to write and create the rails files for you, and here you will find the subdirectory mocks (mock files), unit tests (unit), fixtures (fixture file), and functional tests (functional).

Tmp:  ruby on rails uses the directory which holds the temporary data files only used for intermediate processing.

Vendor: This vendor subdirectory library usually provided by third-party vendors (like security libraries, and database utilities).

Apart from these files, there are two more directories available in demo files:

README: this file contains basic details like rail applications and a description of the directory structured files.

Rakefile: this subdirectory is similar to UNIX Make files, and this helps in building, packaging, and testing the ruby rails codes. This type of demo file used by rake utility approaches and also supports Ruby installation.

Creating ruby on rails application:

In this section, we are going to explain how to create your first ever ruby applications:

Workflow for creating rails applications:

Important points to remember:

  1. Try to use the rails command to create any basic application skeleton.
  2. always create a database on the PostgreSQL server to store and hold your data sets.
  3. Now it’s time to configure the application to know your database application is situated and the login details for it.
  4. Now create the Rails active records (models), because usually, they are available in the form of business objects when you are working with your controllers.
  5. Now create the migration that creates and maintains the database tables and columns.
  6. write your own controller codes to your database application.
  7. Generate the views to represent the data via the user interface.

Creating an empty rails web application:

Steps included are:

1. First go to the ruby installation directory and create your application.

2. Now run the following command to generate a skeleton for the library application. This will create the file directory structure in the domain directory.

The syntax looks like this way:
tp > rails new library

This will generate the subdirectory for the library database application that holds the complete directory tree structure folder and file directory for an empty ruby on rails application.

Starting web server:

Ruby on rails application will run under virtual web server application server, this is considered to be the most convenient way to generate a ruby web application. The ruby web application is used to build the WEBrick web server. The server will be started from the application directory as follows:

Here the port number is 3000:

tp > cd ruby \library

tp \ruby\library\> Rails server

This syntax generates the auto code:

$ rails server

DL is presented, now please use the fiddle

= > booting WEBrick application

= > rails 4.2.3 application starts in the development on HTTP : //localhost:3000

= > run “rails server –h” for more project startup options

= > ctrl – C to shut down the server application

This will start the WEBrick web server application.

The output is as follows:
IMAGE

Ruby on rails-migration details:

Ruby on rails migration allows you to make use of ruby to perform any type of modification in data schema and also helps to control the setup application.
The following are the very important concepts of migration;

1. Terms of the developer: in this section, if one person makes any schema changes, then the other developer will updater and run the application with the help of “rake migrates”.

2. Production server: after the “rake migrates” step, this will roll out the new release to bring the database application.

3. Multiple machines: once you develop this application on both desktop and laptop, and migrations on rails will help you to keep all the applications to perform synchronization.

Functions:

1. create_table (name, options)

2. Drop_table (name)

3. rename _ table (old_name, new_name)

4. Add_column (table_name, column_name, and types)

5. Rename_column (table_name, column_name, and new_column_name)

6. Change_column (table_name, column_name, and type)

7. Remove_column (table_name, and column_name)

8. add_index (table_name, column_name, and index_type)

9. Remove_index (table_name, and column_name)

This type of migration supports different data types. They are;

1. String: this data type is used for small data types namely title.

2. text: this data type is used to store textual data like the description.

3. Integer: used to store whole numbers.

4. Float: used to store decimals.

5. Datestamp and timestamp: used to store the date and time values into a column.

6. date and time: used to store either date value or time value.

7. binary: used to store the data such as images, movies, and audio.

8. Boolean: used to store true or false values.

Valid column options are:

1. limit (: limit = > “50”)

2. default (: default = > “blah”)

3. null (: null = > false implies NOT NULL)

Ruby on rails – scaffolding:

Scaffolding in ruby on rails used to provide more than one cheap demo thrills. Few details regarding scaffolding:

1. With the help of scaffolding you can quickly get the codes in front of your feedback.

2. Here you will be motivated by quicker success.

3. Scaffolding helps to learn how ruby on rails works by generating the codes.

4. In any situation, you can make use of scaffolding as a foundation to start web development.

Scaffolding example:

The syntax is as follows:

Tp > rails new cookbook
Setup the database:
Here is the code:
MySQL > create database COOKBOOK,
Query OK, 1 row affected (0.01 secs)

MySQL > grant all privileges on cookbook. *
To “root” @” localhost” identified by “password”;
Query OK, 0 rows affected (0.00 sec)
MySQL > FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)
Now it’s time to perform scaffolding stages:
Development:
Adapter: MySQL
Database: Cookbook
Username: root
Password: [password]
Host: localhost

Test
Adapter: MySQL
Database: Cookbook
Username: root
Password: [password]
Host: localhost

Production
Adapter: MySQL
Database: cookbook
Username: root
Password: [password]
Host: localhost

Then you will get the scaffold code as follows:

Ruby on rails – scaffolding

Ruby on rails- AJAX:

AJAX stands for Asynchronous javascript and XML. AJAX is not a single technology but it’s a combination of several technologies.

AJAX technology incorporates the following:

  1. XHTML for the markup web page
  2. CSS for the style cascading
  3. Dynamic display system and interaction with the help of DOM
  4. Data manipulations and data interchange using an XML markup language.
  5. Data retrieval with the help of XMLHttpRequest
  6. JavaScript as the glue that helps to incorporate all these together.

Subscribe to our youtube channel to get new updates..!

AJAX technology enables you to retrieve the data for several web pages without the need for any content refreshment. In the basic AJAX architecture, the user can click or submits a form. The form will be submitted to the server, and this sends back any response. Then the response will be displayed on the new page.

How Ruby on rails implements AJAX:

Ruby on rails is a simple, consistent model for implementing AJAX operations. Once the web browser renders the data and displays the initial web pages.

The following are the AJAX operations:

1. Trigger fires:

This trigger could be the user by clicking on a button or link, and then the user makes changes to the data in a field.

2. The web client calls the server:

There are different methods available such as JavaScript Method, XML HttpRequest, and sends data associated with the trigger to perform action handling task. Here the data might be of type ID, entry field, and the text.

3. The server does processing:

The server-side action handler or rails controller action does something with available data and returns the HTML fragments.

4. The client receives the response:

The client-side javascript, where a rail creates automatically and receives the HTML fragment. This will update the current page HTML using <div> tag.

Ruby on rails – Active records:

This ruby on rails active record is the object or relational mapping (ORM) layer supplied with rails. This closely follows the standard ORM model:

  1. Tables map to several classes.
  2. Rows maps to objects.
  3. Columns map to several object attributes.

Translating a domain model into SQL:

Translate the domain model into SQL is generally a straight forward method. Here you can write user-friendly rails-SQL.

You to follow the following rules:

  1. Each entity (for example books) gets a table in the database named after it and plural name forms.
  2. Each data entity matching table consists of a field namely id, this contains a unique integer for each data record inserted into the data table.
  3. Given data entity x and entity y, if the entity y belongs to entity x, then it consists of the field called x_id.
  4. The bulk data fields store the values for entity properties.

Creating active record files:

To create the active record files you have to use these commands:

Library \> rails script/ generate model book]

Library \> rails script/ generate model subject

Creating active record files

You can get the content by using the following command:

Class book < activerecord:: base

End

The content is available in the subject.rb –

Class subject < ActiveRecord :: Base

End

Ruby on Rails Certification Training

Weekday / Weekend Batches

Creating associations between models:

When you are working with more than one model in your ruby on rails application, then you need to create the relationship between these models.

1. One-to-one:

A one-to-one relationship can exist if one item already exists in another item.

2. one-to-many:

A one-to-many relationship exists when a single object can exist or member of many other objects.

3. Many-to-many:

A many-to-many relationship exists when the first object is related to one or more of the second object, and vice versa.

Conclusion :

In this ruby on rails tutorial, you will be learning the definition, architecture, invention, MVC frameworks, Components, Scaffolding, and the advantages. Learning this tutorial will help you to gain knowledge in the Ruby on rails framework. To enhance your industry-oriented knowledge in this tool, you have to take any online course related to technology and this will also help you to get hands-on experience.We offer the best training in the current market and also our course contents are designed by the SME team. Our technical team also provides 24/7 online support for any course-related queries.

Find our upcoming Ruby on Rails Certification Training Online Classes

  • Batch starts on 14th May 2021, Fast Track batch

  • Batch starts on 18th May 2021, Weekday batch

  • Batch starts on 22nd May 2021, Weekend batch

Global Promotional Image
 

Categories

Request for more information

Webinar

Python tutorial for beginners

5th April | 08:00 AM

150 Registered

Kavya Gowda
Kavya Gowda
Research Analyst
Kavya works for HKR Trainings institute as a technical writer with diverse experience in many kinds of technology-related content development. She holds a graduate education in the Computer science and Engineering stream. She has cultivated strong technical skills from reading tech blogs and also doing a lot of research related to content. She manages to write great content in many fields like Programming & Frameworks, Enterprise Integration, Web Development, SAP, and Business Process Management (BPM). Connect her on LinkedIn and Twitter.