A minimal app to give you a taste of Sinatra

Han Lee
2 min readSep 30, 2016

--

I wanted to build a very small app so I can remember how Sinatra works. This is how I want the app to work. You go to the root directory of this app, and you will see a welcome sign and will be asked to fill in a box with a name of a pokemon you want to capture. You type in “Dragonite” and click a submit button, which will lead you to a page showing “You caught Dragonite”.

The app will have three files, one ruby file and two erb files for views.

example.erb

You need rubygems and sinatra to run this, and set :views to ‘views’ to make the app look for view files (erb files) in ‘views’ folder.

When a user visits the root directory (get ‘/’ do), the app will render ‘index.erb’.

index.erb

In index.erb, there is a form to take pokemon’s name. The form element’s action attribute specifies the url (action=”/capture”) it is sent to. In example.erb, params will be stored in the local variable, pokemon_name, and the user will be redirected to “/pokemons”/<your input>.

The line, get ‘/pokemons/:pokemon_name’ do, and the following three lines will store <your input> in the instance variable, @pokemon, and render show.erb. The rendered file, show.erb, can display the pokemon’s name using the instance variable.

show.erb

To start this app, you can type ruby example.rb in your terminal. Happy Coding!

--

--