I just made my first Sinatra ruby app. I love how quickly you can set up Sinatra and put an application on the web. But one of the things that took me a little while to figure out was how to maintain state across routes. Instance variables reset every time you call a new route so it wasn’t obvious at first how you keep track of variables across the program.
Through trial and error and lot of Google searching, I discovered two ways to pass variables in Sinatra without using sessions and without directly passing a variable through a form: query strings and hidden form fields.
According to Wikipedia, a query string is the part of a URL that contains data to be passed to web applications. It looks like this: http://www.getlaura.com/blog?name=“Laura”&page=56. The query string is the part of the URL after the question mark. As you can see there are variables and values separated by ampersands. In this case the variables that I want to keep track of are :name and :page. The corresponding values are “Laura” and 56.
Here’s an example of how to build a query string to use in a route.
<form action=“/?page=<%= page %>“ method=”post”>
Please enter your information.
<input type=”text” name=“name”>
<input type=”text” name=“phone_number”>
The form above assumes that you already have a page variable that you’d like to pass to the application. Your query string will look something like this in the URL:
Hidden Fields in Web Forms
The primary use of query strings is to contain the content of an HTML form, also known as web form. When a form containing the fields name, page, and title is submitted, the content of the fields is encoded as a query string such as: name=“Laura”&page=56&title=“Example”
This query string is passed to your application from the web without adding parameters to the URL. In Sinatra, you can simply extract the values by parsing the params hash. For instance params[:name] = “Laura” when you post this form to your application.
You can purposefully pass variables through your form without adding them to your URL by including hidden fields in your form. This way the variables are added to the query string, but the user will never see them in the form. Here is an example of a form with hidden fields:
The form above assumes that you already have a page variable that you’d like to pass to the application. Your query string will look something like this but you won’t see it in the URL:
Formatting Query Strings
Not all characters can be used in a url or query string (an obvious example being the space). There are some characters with a special mean (‘#’ is reserved for a spot on a page and ‘=‘ is reserved for field/value pairs.
In particular, encoding the query string uses the following rules:
* Letters (A–Z and a–z), numbers (0–9) and the characters ‘.’,’-‘,’~’ and ‘_’ are left as-is
* SPACE is encoded as ‘+’ or “%20”
* All other characters are encoded as %HH hex representation with any non-ASCII characters first encoded as UTF-8 (or other specified encoding)
I found it difficult to pass arrays through as values because of these formatting specifics. So keep these in mind if you are running into problems passing values.