Page Content

Tutorials

Building Your First Website Sinatra In Ruby With Examples

Your First Website Sinatra in Ruby

Building your first website using Sinatra involves installing the necessary library and creating a simple project that handles web requests. Sinatra is a Ruby framework designed especially for web development.

Setting Up and Running Sinatra

You need to finish installing Sinatra before you can start building your first website.

Installation

The installation of Sinatra is the first necessary step. Frequently, the command line (Terminal or PowerShell) is used to install applications (such as gems). Installing it will allow you to create a basic “Hello World” project.

You can also read What Is Command Line Applications In Ruby With Examples

Basic Website Generation Concept

For the minimal Sinatra “Hello World” project, the objective is to run Ruby code that produces HTML output, however the snippets do not include the precise Ruby code.

A basic web script, such as a CGI script, typically prints headers and HTML content to standard output.

Example of Minimal Web Output (CGI Script, illustrative of website output):

#!/usr/bin/ruby
print "Content­type: text/html\r\n\r\n"
print "<html><body>Hello World! It's #{Time.now}</body></html>\r\n"

It is required that this script be declared as executable and stored in a CGI directory. When viewed with a web browser, “Hello World! [Currently] is the time.

Controlling the Application

When you are working on a Sinatra website, you must understand how to pause and restart the program. Resolving faults that may arise during development is also crucial.

You can also read What Is RDoc In Ruby? The Official Ruby Documentation System

Working with Templates and Input

In web development, templates are essential for separating the application logic (Ruby code) from the display (HTML).

Creating Templates

Setting up the initial application is followed by the creation of simple templates. In the process, a Layout Template is also created. With the help of templates, plain text and Ruby statements can be combined to generate documents, frequently HTML.

Handling Browser Input

In the following steps of creating a web application, you will learn how to get input from a browser. To do this, you must comprehend how forms and the web operate. The program that listens for requests on the Internet is called a web server, and forms are necessary to collect user data. A web browser is used to send requests to a web application.

In order to capture this input, HTML forms must be created. In general, HTML forms have components like a submit button and text input portions. User-inputted data is returned to the script as CGI parameters upon form submission.

If you were to use a tool such as the Ruby CGI class to generate a basic form, the HTML that would be produced may look like this:

<HTML><HEAD><TITLE>This Is a Test</TITLE></HEAD>
<BODY><h1>This Is a Test</h1><hr>
<FORM METHOD="post" ACTION="/test.cgi">
<INPUT NAME="get_text" TYPE="text"><BR><INPUT TYPE="submit"></FORM></BODY></HTML>

The user’s input is sent to the script as a CGI parameter called get_text when this form is submitted.

Automated Tests for Forms can also be written as part of the form processing process.

Next Steps: Building a Web Game

Building more intricate applications, like the beginning of your web game, is possible once you have mastered the fundamentals of input and output. This could entail reworking the Game’s old game code for a web environment.

It is frequently necessary to track users and manage sessions while creating a game. A session is data that needs to be kept across requests from a certain browser. Users’ interactions with the game can be tracked by the application through sessions.

Developing an engine and occasionally using HTML for the session-based game engine are additional tasks involved in the web game.

You can also read RubyGems Explained: Installing And Managing Ruby Libraries

Agarapu Geetha
Agarapu Geetha
My name is Agarapu Geetha, a B.Com graduate with a strong passion for technology and innovation. I work as a content writer at Govindhtech, where I dedicate myself to exploring and publishing the latest updates in the world of tech.
Index