What are Params in Sinatra

Erica Grundy
2 min readDec 31, 2020

Params is another way to refer to Parameters, and indicate the parameters being passed to the controller via a GET or POST request.

It is a hash that stores the key-value pairs that were encoded inside of a URL/HTTP request. This hash is available for us in our route blocks and holds data that can be passed around.

We can access the data in different ways. One of them is to access it using forms. Let’s take a look at the following form, part of my “Signup” form from my Sinatra Application.

Here we are guiding our form on how to manipulate the data from the user, and what route the POST request should be sent to. In this example, we are posting to a route called /users/signup. After the request, the server receives the data as a hash with its keys and values pairs, which then will be accessible from the params hash. This params hash would look like this:

Below, when we look at the POST route, at the second line of code, we can see that whenever the user submits the form, it will be possible to access the data the user submitted because the controller would pass, to the create method, the following hash: :user => { :name => “Erica”, :email => “erica@mail.com”, :password => “rth$%&6kjslmmrwgrt#$”}.

User.create(params) will become @user = User.new(name: “Erica”, email: “erica@mail.com”, password: “rth$%&6kjslmmrwgrt#$”).

In conclusion, the data submitted is passed as a hash with its keys and values pairs (params). If we take a look at the form above, the key will be the name of the data (name=”name”, name=”email”, name=”password”), and the value will be whatever data the user submits, that is successfully stored in the database.

--

--