Table of contents
1.
Introduction 💁
2.
What is Sinatra 🤔
2.1.
Code 📝
2.2.
Output
3.
Filters
3.1.
Before Filter
3.2.
After Filter
3.3.
Example
4.
Helpers
4.1.
Code 📝
4.2.
Output
5.
Sessions
5.1.
Security in Sessions
6.
Frequently Asked Questions
6.1.
What are web frameworks other than Sinatra?
6.2.
What are gems in Ruby?
6.3.
What is an API in Sinatra?
6.4.
What is a Domain Specific Language(DSL)?
6.5.
What is Ruby?
7.
Conclusion
Last Updated: Aug 13, 2025
Easy

What Are Helpers And Filters in Sinatra

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction 💁

A web application is a program stored on a remote server and delivered over the Internet through a browser interface. Creating a web application has become a significantly easier task nowadays because of the different Web frameworks present in the industry. Sinatra is one of the web frameworks written in Ruby language that can be used to create Web Applications and services.

Introduction

What is Sinatra 🤔

Sinatra is an open-source framework present in Ruby that is used for Web application development. Sinatra is considered an alternative to other Ruby frameworks such as Merb, Ruby on Rails, Camping, etc. Let us look at a basic code snippet of Sinatra. We will write a simple code to print “Welcome to CodingNinjas”.

What is Sinatra

Code 📝

#Fetching the Sinatra package in our code.
require 'sinatra'
#Fetching the root route
get '/' do
  #Setting the content as Welcome to CodingNinjas.
  'Welcome to CodingNinjas'
end
You can also try this code with Online Ruby Compiler
Run Code

Output

Introduction Console

In our example, we specify the port as 3000.

Introduction Output

Filters

There are two types of filters available in Sinatra, namely,

  • Before Filter
  • After Filter

We will discuss both of these filters in this section.

Before Filter

The before filter in Sinatra is used to manipulate the request and the responses before the function calls or request calls are executed.

After Filter

The after filter in Sinatra is used to manipulate the request, and the responses after the function calls or request calls are executed.

Now that we know about filters let us look at an example of filters in Sinatra.

Example

require 'sinatra'


after do
  puts "This call is through after filter"
  puts response.status
  def bar(name)
      "#{name}"
    end
  puts ""
end


before do
  @note = "Hey Ninja!!"
  puts "This call is through before filter"
  puts @note
  def bar(name)
      "#{name}"
    end
  puts ""
end



get '/' do
  @note #=> 'Hi!'
  bar("Hey Ninja!!") #=> 'bar/baz'
end
You can also try this code with Online Ruby Compiler
Run Code
Filters Console Output
Filters Output

Helpers

Now that we know about filters let us look at what helpers are in Sinatra. The helper module is used to create custom templates in Sinatra. A standard example of helpers would look something like this,

Code 📝

require 'sinatra'
#Creating a helper template.
helpers do
  def bar(name)
    "#{name}"
  end
end
#Specifying the root route that takes a name as the parameter.
get '/:name' do
  bar(params['name'])
end
You can also try this code with Online Ruby Compiler
Run Code

Output

Helpers Console

In our example, we have set the port as 3000 again.

Helpers Output

We can also create a separate module in Sinatra to define our helper methods. For example,

#defining the first helper named FirstName which displays the first name of the user.
module FirstName
  def fName(name) 
    "First name is: #{name}" 
  end
end
#defining the second helper named LasrName which displays the last name of the user.
module LastName
  def lName(name) 
    "Last name is: #{name}" 
  end
end


helpers FirstName, LastName
You can also try this code with Online Ruby Compiler
Run Code

Sessions

session is used to keep the state intact while processing requests. Sessions are also used to store the data for a particular client, keeping the details intact for future use and references. Every time you run the website new unique session id is created. Let us look at an example to fetch the session id of a web page,

require 'sinatra'

//Enabling sessions for the webpage
enable :sessions

#Displaying the session id on the route page
get '/' do
  "value = " << session[:value].inspect
end

#Assigning a session id using the URL parameters
get '/:value' do
  session['value'] = params['value']
end
You can also try this code with Online Ruby Compiler
Run Code
Session ID output 1

Initially, the session id is set to nill. Now let us assign a session id using the URL parameters.

Session ID output 2

Now closely observe the URL. We have passed RandomSessionID as the parameter. This will now be set as the session id for this session. Let us display the session id again using the root route.

Session ID Output 3

As you can see, our URL parameter has been assigned as the new session id for this session.

Security in Sessions

Sessions can be a very personal thing, as the session id might be associated with the data of the session. So to keep a session secured is very important. Sessions in Sinatra are automatically generated in a 32-bit secured session secret which is changed with every restart of the application. We can also manually generate a 64-bit secured session id using the following code in ruby,

ruby -e "require 'securerandom'; puts SecureRandom.hex(64)"
You can also try this code with Online Ruby Compiler
Run Code
Output Session ID

 

We can also use the sysrandom gem present in ruby to generate a secured session id. We can install the sysrandom gem using the following code snippet,

gem install sysrandom
You can also try this code with Online Ruby Compiler
Run Code

 

After downloading the sysrandom gem, we can use the following code snippet to generate a random secured session id,

ruby -e "require 'sysrandom/securerandom'; puts SecureRandom.hex(64)"
You can also try this code with Online Ruby Compiler
Run Code

 

After generating the secured session id, we can set it as secret using the Session_Secret variable.

 

Now let's discuss some frequently asked questions associated with Vaadin Spring Scopes.

FAQs

Frequently Asked Questions

What are web frameworks other than Sinatra?

Some popular web frameworks besides Sinatra are Ruby on Rails, Django, Flask, Express, Spring, etc.

What are gems in Ruby?

Gems are equivalent to packages in other programming languages. Gems are packages that are used to perform specific tasks. RubyGems is used to manage these packages in Ruby.

What is an API in Sinatra?

API stands for Application Programming Interface. It is a software intermediary that allows two applications to talk to each other.

What is a Domain Specific Language(DSL)?

A domain-specific language (DSL) is a computer language specialized for a particular application domain.

What is Ruby?

Ruby is a high-purpose programming language that is used for multiple programming paradigms. The main emphasis of Ruby is on simplicity and programming productivity.

Conclusion

This Blog covered all the necessary points about the helpers and filters in Sinatra. We also briefly discussed about sessions in Sinatra. We further discussed various codes associated with helpers, filters, and sessions in Sinatra.

Hey Ninjas! Don’t stop here. Check out Coding Ninjas for Python, more unique courses, and guided paths. Also, try Coding Ninjas Studio for more exciting articles, interview experiences, and excellent Machine Learning and Python problems.

Live masterclass