Some Useful Ruby Tips Worth Trying

In this blog I’m going to share some ruby (or ROR) tips that I found helpful and somewhat awesome. As compared to other programming, in ruby we can implement any logic in lesser lines of code. So here is the first tip:-

1. Many a times while working on some application in rails we write(try out) long active record queries which run successfully but at the same time we forget to assign the result of the query to a variable. Again to do that we press the UP key then left key until we get back to start point of the query to declare a variable and assign the result to it. But not many of us know there’s a way to do that in no time. See an example below

Continue reading


Introduction to Git

Git is a free and open source distributed revision control and source code management system designed to handle projects with speed and efficiency. It is initially designed and developed by Linus Torvalds to maintain the source code of Linux kernel development in 2005. Following goals were kept in mind during the development of Git :-

  • Speed
  • Simple design
  • Strong support for non-linear development
  • Fully distributed
  • Able to handle large projects

To use Git effectively, it would be better to understand what Git is and how it works. Git stores and thinks about information much differently than the other VCS systems, even though the user interface is fairly similar; understanding those differences will help prevent us from becoming confused while using it. So now lets go through the advantages of Git.

The most important difference between Git and other VCS is the way Git thinks about its data. Most of the other version control systems store information as a list of file based changes. These systems keep a list of files and everytime the information is changed they keep the changed file with the set of previous file version. But Git thinks different, it thinks of its data more like a snapshot of the mini file system. When a user commits or save state of project on Git, it takes a picture of what all files look like at that moment and stores a reference to that snapshot. If files have not changed, Git doesn’t store the file again, it just creates a link to the previous identical file it has already stored. As now we know the difference between Git and other VCS, lets Move on the second important feature of Git.

Nearly every operation on Git is local, Most of the operations in Git only needs local files and resources. Here even if the user is not connected to the internet, s/he can commit the changes to the local database but in other version control systems (VCS) like Subversion or CVS, user can edit the file but can’t commit the changes if s/he is offline. So now as we know about this powerful feature of Git, lets move on to the next feature which tells about the integrity of Git.

In Git, if user changes content of a file then it will be recorded by Git. It’s impossible to change the contents of a file without Git knowing about it. Git uses SHA-1 mechanism for this checksum feature. Git even stores file by the SHA-1 hexadecimal value of its contents and not by its name. So now we know that how powerful Git is in terms of security. Lets move on to the next feature of Git.

Git generally only adds data, As in any VCS, a user can lose or mess up changes s/he has not committed yet, but after user commits a snapshot into Git, it is very difficult to lose, especially if user regularly pushes his/her database to another repository.

After going through few of the features of Git, we are quite convinced that Git is the most powerful SCM tool available today. Now as we know the basics of the Git, in our next blog we will proceed with the stages of Git and its installation. Thanks for reading the blog!

Templates in Rails Views : An Overview

My previous blog gives a brief introduction to Rails framework, Model-View-Controller architecture and Rails Views. There I explained briefly about the three components of Rails Views, namely Templates, Partials and Layouts. In this blog we will go a bit deeper into the first essential component of Rails Views, i.e., Templates. As now we know, Rails Action Views use template systems based on Ruby library ERB or HAML which create a presentation layer for the end user, let’s proceed with Templates.

In a controller, in one action we can render data in multiple formats. We can use respond_to and pass the formats in the block we want to see in the view. For this blog, let’s take index action. Below is the code snippet which gives an idea about how to define a respond_to block.

respond_to do |format|





As we know there are many ways of writing Action View templates. If .erb extension is used then it is a mixture of HTML and ERB or JS and ERB. But, if .builder extension is used then we are rendering data in XML format using an instance of Builder::XmlMarkup library. Let’s one by one go through the things we can do using Rails Views.

The first one is the most common one, i.e., displaying an HTML format page using ERB. Let’s say we want to print Hello world! five times on the page. Now in normal HTML pages we need to repeat the text five times, but in Rails, using ERB we can use Ruby loops to simply repeat the text five times on the page. For that, in the View directory which is associated with the controller, we need to create a view file named index.html.erb, and in that file we need to write the code. Below is the code snippet which explains how we can achieve this in Rails.

<% 5.times do %>

  <p> Hello world! </p>

<% end %>

The above code explains that we can execute Ruby codes inside <%  %> tags in Rails Views. We can also define conditions to show or hide a text using Ruby’s if or unless. These are the two most common things we can do in Rails Views. In our upcoming blogs we will discuss about that in deep.

The second format is XML, let’s say we want to render data in XML format, as we know, to render XML data Rails uses instance of Builder::XmlMarkup library. So in the same way , in the View directory associated with the controller we need to create a view file named index.xml.builder, and in that file we need to write the code. Below is the code snippet.


xml.messages do

  2.times do

    xml.text "Hello world!"



The output of the above code snippet is


  <text>Hello world!</text>

  <text>Hello world!</text>


So, now we know how to render data in XML format in Rails, dead simple, no?!

Moving on to the next format, i.e., Javascript. Rails by default uses UJS, where making AJAX calls are very simple. We just need to add remote: true to the link or form, and then we are done. Let’s say we want to alert Hello world! when a user clicks on a link. Let me explain it with a quick example. In some other view, we will add a link which will point to the index action we are working on.

<%= link_to "Click Me!", "controller/index", remote: true %>

We will go in details about the link_to method later. For now let’s create a view in the same directory we used in earlier two examples, name of the view should be index.js.erb, here we can write JS codes along with the embedded ruby. Below is the code snippet.

<% if true %>

alert("Hello world!");

<% end %>

When the Click Me! button is clicked, an alert box will popup with text Hello world!

Now we can say that we are familiar with Rails Templates, in upcoming blogs we will go in details with other components of Rails Views, Till then, stay connected. Thanks for reading the blog!

Introduction to Rails Views


Ruby on Rails, or we often call it as Rails, is an open source web application framework written in Ruby programming language. As we all know Rails framework follow MVC architecture. MVC refers to Model-View-Controller. Let us try to understand what is the job of Model-View-Controller.

In Lehman’s term, View is what we see on the screen, Model is the one which fetches or store information in the database and Controller acts as a middle man which connects View and Model. When we click some link, or submit a form on a web application, the request goes to the action in controller, then controller interacts with model if some data needs to be fetched or stored in the database. After the successful execution of the code in the controller, the corresponding view is rendered which shows the data to the end user on an HTML page. In rails, Views are…

View original post 432 more words