Ruby on rails Tutorials

Ratings:
(4)
Views:0
Banner-Img
  • Share this blog:

 

Welcome to Ruby on rails Tutorials. Ruby on Rails is an extremely productive web application framework written in Ruby by David Heinemeier Hansson. This tutorial gives you a complete understanding of Ruby on Rails.

In addition to Ruby on rails tutorials, we will cover common interview questions and issues of Ruby on Rails.

Introduction

Ruby is a programming language. It was created 20 years ago by Yukihiro “Matz” Matsumoto. By most measures of programming language popularity, Ruby ranks among the top ten, though usually as tenth (or so) in popularity, and largely due to the popularity of Rails. Like Java or the C language, Ruby is a general-purpose programming language, though it is best known for its use in web programming.

Ruby is the successful combination of Smalltalk conceptual elegance, Python's ease of use and learning, and Perl's pragmatism.

Ruby is a high-level programming language which Interpreted like Perl, Python, Tcl/TK, and Object-oriented like Smalltalk, Eiffel, Ada, Java.

Rails:

Rails is a software library that extends the Ruby programming language. David Heinemeier Hansson is its creator. He gave it the name “Ruby on Rails,” though it is often just called “Rails.” Rails Strengths is packed with features that make you more productive, with many of the following features building on one other.

  • An extremely productive web-application framework
  • Written in Ruby by David Heinemeier Hansson.
  • You could develop a web application at least ten times faster with Rails than you could with a typical Java framework.
  • An open-source Ruby framework for developing database-backed web applications.
  • Configure your code with Database Schema.
  • No compilation phase required

Inclined to build a profession as Ruby on Rails Developer? Then here is the blog post on, explore Ruby on RailsTraining

Sample Ruby Code

Here is a sample Ruby code to print "Hello Ruby"

# The Hello Class
class Hello
   
   def initialize( name )
      @name = name.capitalize
   end

   def salute
      puts "Hello #{@name}!"
   end
   
end

# Create a new object
h = Hello.new("Ruby")

# Output "Hello Ruby!"
h.salute

Output − This will produce the following result
Hello Ruby!

Embedded Ruby

  • ERB (Embedded RuBy) is a feature of Ruby that enables you to conveniently generate any kind of text, in any quantity, from templates. The templates themselves combine plain text with Ruby code for variable substitution and flow control, which makes them easy to write and maintain.
  • Although ERB is most commonly seen generating Web pages, it is also used to produce XML documents, RSS feeds, source code, and other forms of a structured text file. It can be extremely valuable when you need to create files that include many repetitions of a standard pattern, such as unit test suites.
  • The main component of ERB is a library that you can call within your Ruby applications and Rake tasks. This library accepts any string as a template and imposes no limitations on the source of the template. You may define a template entirely within your code, or store it in an external location and load it as required. This means that you can keep templates in files, SQL databases, or any other kind of storage that you want to use.
  • Here's an example. Save the code in webdemo.rb file. Note that a Ruby file will have an extension .rb
<% page_title = "Demonstration of ERB" %>
<% salutation = "Dear programmer," %>

<html>

   <head>
      <title><%= page_title %></title>
   </head>
	
   <body>
      <p><%= salutation %></p>
      <p>This is an example of how ERB fills out a template.</p>
   </body>
	
</html>

Now, run the program using the command-line utility erb

tp> erb erb demo.rb

This will produce the following result −

<html>

   <head>
      <title>Demonstration of ERb</title>
   </head>
	
   <body>
      <p>Dear programmer,</p>
      <p>This is an example  of how ERb fills out a template.</p>
   </body>
	
</html>

Workflow for Creating Rails Applications

A recommended workflow for creating Rails Application is as follows −

  • Use the rails command to create the basic skeleton of the application.
  • Create a database on the PostgreSQL server to hold your data.
  • Configure the application to know where your database is located and the login credentials for it.
  • Create Rails Active Records (Models), because they are the business objects you'll be working within your controllers.
  • Generate Migrations that simplify the creating and maintaining of database tables and columns.
  • Write Controller Code to put a life in your application.
  • Create Views to present your data through the User Interface.

Rails Installation

Installing Ruby on Rails

Step 1: Check Ruby Version

First, check if you already have Ruby installed. Open the command prompt and type ruby -v. If Ruby responds, and if it shows a version number at or above 2.2.2, then type gem --version. If you don't get an error, skip the Install Ruby step. Otherwise, we'll install a fresh Ruby.

Step 2: Install Ruby

If Ruby is not installed, then download an installation package from rubyinstaller.org. Follow the download link, and run the resulting installer. This is an exe file rubyinstaller-2.2.2.x.exe and will be installed in a single click. It's a very small package, and you'll get RubyGems as well along with this package. Please check the Release Notes for more detail.

Ruby on Rails Interview Questions

Step 3: Install Rails

Install Rails − With Rubygems loaded, you can install all of the Rails and its dependencies using the following command through the command line.

C:> gem install rails

Install rails

Note − The above command may take some time to install all dependencies. Make sure you are connected to the internet while installing gems dependencies

Step 4: Check Rails Version

Use the following command to check the rails version.

C:> rails -v

Output

Rails 4.2.4

Configuring Rails Applications

First, we define a default value for all environments in config/application.rb:

module Configurator
  class Application < Rails::Application
    # By default, let OSX resolve the path to the binary
    config.wkhtmltopdf = "wkhtmltopdf"
  end
end
 
Then we override the default setting as necessary in config/environments/:
 
Configurator::Application.configure do
  # Settings specified here will take precedence over those in config/application.rb
  # Point Heroku explicitly to the binary we need to use
  config.wkhtmltopdf = "#{Rails.root}/bin/wkhtmltopdf"
end
 
Lastly, we access the configuration element in our code:
 
cmd = [Configurator::Application.config.wkhtmltopdf, url, tmpfile.path]
 
Just use Rails environment support and config to store your own configuration elements. They’re trivial to set, trivial to access, and require no third-party gems or custom text files.

Advantages of Ruby on Rails

  • High Speed of Development: For example, site development that would traditionally take about 2-3 months can commonly be launched much quicker with Ruby on Rails. This time savings results from a few factors: a leaner code-base (fewer lines of redundant code) and a modular design (re-using existing components rather than building everything from scratch). As such, after launching the site, modifications can be made more quickly as well.
  • Cost-Effectiveness: As Rails sites can be built and modified quickly, this is a really cost-effective way to create and maintain the website — without compromising the quality, performance, or scalability of your site.
  • Flexibility: There are lots of gems (already built components of specific functionality available); re-using them rather than building everything from scratch provides a lot of different bricks to build the application and extend its functionality. A variety of plugins are available to solve just about any functional challenge you may need for your project. Additionally, Rails is an ideal way to build custom database solutions at a fraction of the usual time and expense.
  • Flexibility: There are lots of gems (already built components of specific functionality available); re-using them rather than building everything from scratch provides a lot of different bricks to build the application and extend its functionality. A variety of plugins are available to solve just about any functional challenge you may need for your project. Additionally, Rails is an ideal way to build custom database solutions at a fraction of the usual time and expense.
  • High code quality: The Ruby language and Ruby on Rails framework are under active development and support. New features, ideas are introduced quite often. Security updates and fixes are released regularly. A large body of books, blog entries, and experiences are also shared online to learn from.
  • Industry support: There are professional hosting support companies, (Heroku, EngineYard). experienced consulting companies, two primary cloud-based offerings, and help with development and deployment, and more. Both provide an easy-to-scale, managed hosting environment. Both are built on Amazon EC2 and offer contrasting approaches and features that will appeal to different audiences.

 

You liked the article?

Like : 0

Vote for difficulty

Current difficulty (Avg): Medium

Recommended Courses

1/15

About Author
Authorlogo
Name
TekSlate
Author Bio

TekSlate is the best online training provider in delivering world-class IT skills to individuals and corporates from all parts of the globe. We are proven experts in accumulating every need of an IT skills upgrade aspirant and have delivered excellent services. We aim to bring you all the essentials to learn and master new technologies in the market with our articles, blogs, and videos. Build your career success with us, enhancing most in-demand skills in the market.


Stay Updated


Get stories of change makers and innovators from the startup ecosystem in your inbox