How params work on Sinatra

Ingrid Fuentes
2 min readOct 25, 2019

--

Working on my Sinatra project, I learnt about how parameters (or params) work and why it is important to know about them. Params are important because we can not build a web application without having clear their functionality.

First at all, params work as a hash. That means that contains keys and values. So we can access to its data in different ways. One of them is to access to params through forms.

More about forms …

  • Forms are important because we fill out online forms everyday. Some examples of forms are card payments, logins, registrations forms and so on.
  • Forms are the most common way for users to pass data to a web application.

How do we connect forms to a Sinatra Application?

We connect HTML forms to a Sinatra Application by using forms. These forms take attributes like user’s name or email, for example. Then, this returns an interpolated string. Our form would look like this:

<form action="/login" method="post">
<input type="text" name="username" placeholder="Username" >
<input type="text" name="email" placeholder="Email" >
<input type="password" name="password" placeholder="Password" ><input type="submit" value="Login">
</form>

This form above is part of my “Login” form from my Sinatra Application. We are telling our form where and how to send the data from the user. In other words, with the action attribute we are telling the form what route the POST request should be sent to. In this example, we are posting to a route called /login. The method attribute tells the form the kind of request that should be sent to the server after the user clicks the submit button. But why a POST request? The answer is simple. In general, forms user POST request in order to “post” data to the server.

After the request, the Sinatra server receives the string, and it parses to get the data as a hash with its keys and values pairs. These key/value pairs are accessible from the params hash. Following our example, our hash would look like this:

{"username" => "Sophie",
"email" => "sophie@mail.com"
}

Let’s keep breaking down the form! The next lines refer to <input>. This field must define a “name” attribute that defines how our application will identify each <input> data. Input names will need a username, email, and password.

We also should have clear about the corresponding route every form needs. Then, checking out our controller file, and still following our previous example, we see this:

post "/login" do
@user = User.find_by(:username => params[:username])
if @user && @user.authenticate(params[:password]) session[:user_id] = @user.id
redirect to "/stories"
else
flash.now[:message] = "Invalid Email or Password"
erb :"users/login"
end
end

Well, we have a POST route that it matches the method attribute.

Great!

To sum up, whenever the user submits the form, it will be possible to access the data entered into this: params[:username]. We will be passing the data in the form of a hash where the key will be the name of the data (name=”username”) and the value will be whatever the user enters.

I hope this help you to clarify some points on how params and forms work together.

--

--