1. Creating a basic Sinatra Project
Building your first basic Sinatra project
Guides, tutorials and labs to accompany CMU's Programming for Online Prototypes (49-714).
Everything you need to know about building microservices for the web with Ruby and Sinatra.
We’ve seen how to map routes to actions (blocks of code). And how actions do specific things that return information back to a user. Sometimes these actions need to know more than just the parameters which have been passed. And sometimes, some information might need to be shared across multiple actions.
For example:
We can use the configuration
block in Sinatra to hold settings or other information we might want to make available to multiple routes. This information is simple stuff we need to know and stuff that won’t change anytime soon. It shouldn’t make sense to store in a relational database either.
The configuration block is run once as our web application launches for the first time and can be set up like this:
configure do
# setting one option
set :option, 'value'
# same as `set :option, true`
enable :option
# same as `set :option, false`
disable :option
end
Then you can access this information in any route using settings
configure do
# setting one option
set :application_title, 'My Application Name'
end
get '/' do
# check if the setting exists and is set
settings.application_title?
# get the application title and return it
settings.application_title # => 'My Application Name'
end
This series guides you through creating your first Sinatra project and some of the features it offers for creating web-delivered apps
Building your first basic Sinatra project
Building your first basic Sinatra project
Routing with HTTP Methods explained
How to include named parameters in defined routes
Storing User Information in Sessions
How to return a HTTP error response
Using configuration blocks to store application wide variables