Back End Development with Rails

Sayan Bandyopadhyay
5 min readJul 4, 2019

--

PART 2

by Sayan Bandyopadhyay

If you haven’t read the first part yet, here is the link-

https://medium.com/@SayanBan/back-end-development-with-rails-6002afb5b8e7?source=friends_link&sk=3c984e77fb757c9a2892195367f4b69d

Ruby is associate understood, high-level, general programing language. it absolutely was designed and developed within the mid-1990s by Yukihiro “Matz” Matsumoto in Japan. Ruby is dynamically written and garbage-collected. It supports multiple programming paradigms, as well as procedural, object-oriented, and purposeful programming.

WHY RUBY?

There are many reasons for which Ruby is preferred.Some are mentioned below-

  • It’s a full-stack framework that covers both front and back-end design.
  • Ruby has been used by companies like Twitter, Airbnb, Shopify, Github, Slideshare, Basecamp and Shopify.
  • If a website or app is complex and that involves a database then Rails is the best option. It is the fastest and most efficient way to build apps for the web.
  • Big brands to brand-new startups are tapping RoR developers to get that speed and agility.
  • Scalable and big programs written in Ruby are easily maintainable.
  • Ability to write multi-threaded applications with a simple API.

The Rails Framework uses the language Ruby. So in order to learn Rails, first we need to learn Ruby.

There are some places where we can learn the language.

  1. https://github.com/SayanBan/LEARN-RUBY
  2. https://www.codecademy.com/learn/learn-ruby
  3. https://www.udemy.com/learn-to-code-with-ruby-lang/
  4. https://www.coursera.org/courses?query=ruby

There are many websites where Rails is used as the framework. Some of them are listed below.

These are some of the paid and unpaid ways to learn Ruby.

Now speaking about the Framework, lets start learning.

[First you have to learn the language ruby, only then you will be able to understand the tutorial fully.]

In this Blog, we will just get started with the framework.

Things required-

  1. Bash or Rails enabled windows command prompt
  2. A browser
  3. Your knowledge in Ruby, HTML, CSS and JavaScript.

DON’T KNOW HOW TO INSTALL RAILS?

FOLLOW THIS SITE :-

https://medium.com/@SayanBan/install-ruby-and-ruby-on-rails-on-your-system-93c95359e56d?sk=899921c9aeb86dd1f924fae7a06ff79a

Let’s get started by making a Rails app for a personal website. We’ll explain each step afterwards.

  1. In the terminal, type
$ rails new MySite

2. Then run

$ bundle install

Finally type

$ rails serverThen, view the Rails app in the browser by visiting http://localhost:8000 in the provided browser.

In 3 commands, you designed a operating Rails app that displays the Rails default page. however will it work?

  1. The rails new command created a brand new Rails app named MySite. It generated variety of files and folders that we are going to use to make the app. within the Code Editor, click on the folder icon to visualize these files and folders. We’ll see what these files and folders square measure for within the next exercises. The rails new command is that the start line of each Rails project.
  2. The bundle install command put in all the code packages required by the new Rails app. These code packages area unit referred to as gems and that they area unit listed within the file Gemfile
  3. The rails server command started the Rails development server so we tend to may preview the app within the browser by visiting http://localhost:8000. This development server is termed WEBrick.

CONTROLLER

What happens when you visit http://localhost:8000 in the browser? Check out the diagram in the browser.

  1. The browser makes a request for the URL http://localhost:8000.
  2. The request hits the Rails router in config/routes.rb. The router recognizes the URL and sends the request to the controller.
  3. The controller receives the request and processes it.
  4. The controller passes the request to the view.
  5. The view renders the page as HTML.
  6. The controller sends the HTML back to the browser for you to see.

This is called the request/response cycle. It’s a useful way to see how a Rails app’s files and folders fit together.

Instructions

1.

Looking at the request/response cycle, we need three parts to build a Rails app: a controller, a route, and a view. Let’s start here by creating a controller.

In the terminal, type:

rails generate controller Pages

2.

After rails generate finishes running, in the Code Editor, open app/controllers/pages_controller.rb. Kindly create the files and folders which you are not able to find .Within the class PagesController, add a method home:

class PagesController < ApplicationController   def home
end
end

WHAT IS A REQUEST-RESPONSE CYCLE?

When developing a Rails app, the request/response cycle may be a helpful guide to check however all the app’s files and folders work along. The request/response cycle traces however a user’s request flows through the app. Understanding the request/response cycle is useful to work out what files to edit once developing an app (and wherever to appear once things aren’t working)

  • A user opens his browser, types in a URL, and presses Enter.
  • When a user presses Enter, the browser makes a request for that URL.
  • The request hits the Rails router (config/routes.rb). The router maps the URL to the correct controller and action to handle the request.
  • The action receives the request and passes it on to the view.
  • The view renders the page as HTML.
  • The controller sends the HTML back to the browser. The page loads and the user sees it.

Routes

We created a new controller named Pages. How did we do this?

  1. The rails generate controller Pages command generated a new controller named Pages. This created a file named app/controllers/pages_controller.rb.
  2. Inside the new Pages controller, we added a method called home. Methods in Rails controllers are also referred to as controller actions, so here we added the home action to the Pages controller.

Instructions

1.

Now that we have a controller, let’s move on to the second part of the request/response cycle and create a route.

Open config/routes.rb and type:

Rails.application.routes.draw do
get 'welcome' => 'pages#home'

Views

Now when a user visits http://localhost:8000/welcome, the route

get 'welcome' => 'pages#home'

will tell Rails to send this request to the Pages controller’s home action.

Instructions

1.

Now that we have a controller and a route, let’s move on to the third part of the request/response cycle and create a view.

Open app/views/pages/home.html.erb, and type in the following HTML. Fill in your own name.

<div class="main">
<div class="container">
<h1>Hello my name is Sayan</h1>
<p>I make Rails apps</p>
</div>
</div>

2.

View your app by visiting http://localhost:8000/welcome in the browser.

So what are the thngs we learned in this part?

Using the request/response cycle as a guide, this has been our workflow when making a Rails app.

  1. Generate a new Rails app.
  2. Generate a controller and add an action.
  3. Create a route that maps a URL to the controller action.
  4. Create a view with HTML and CSS.
  5. Run the local web server and preview the app in the browser.

Congratulations! You built a Rails app from scratch.

In the next blog, We will learn about creating apps which saves informations.

To Be Continued…

Continued in :-

https://medium.com/@SayanBan/back-end-development-with-rails-929ad277b716

Thank you.

--

--

Sayan Bandyopadhyay

Full-time Developer, part-time Blogger. Sarcasm is my love language and procrastination is my superpower.