Experimenting with Ruby, Sinatra and PostgreSQL: a Message Board App

We interact with the postgres database software through an interface called “psql.”You can login with user “postgres” to database “postgres” with the following command:sudo -u postgres psql postgres(You can exit with CTRL+D.)Install rerunWe will install rerun, which will make it possible to make changes to the code and see the results without restarting the server.For this, we prepare a Gemfile in the project.touch GemfileAnd the Gemfile should contain the following:source 'https://rubygems.org'gem 'sinatra', '2.0.4'gem 'rerun', '0.13.0'We need to install these packages (we already installed sinatra earlier).bundle install(If the system does not contain bundler, you need to install it with sudo gem install bundler)Now we need to start the server, but for this we use the following command:bundle exec rerun ruby-hello-world-sinatra.rbEarlier we needed to quit and restart the server in order to see changes to the app, but thanks to the rerun the app is updated automatically, after we refresh the browser by hand.Message Board Web AppWhat will the app know?.The app will show the already posted messages, and the nickname of the person who posted the message..It will allow to post new messages..The data will be stored in a database, and the app will read the entries from the database, and save the new posts there.The app is created step by step.1st step: List hard-coded valuesThe first version of the app will show some hard-coded messages without using the database..(Later we will enhance the app to read up messages from the database.) In the first version we will also not able to post messages.DesignI tried to define a nice-looking but simple design with CSS..I used Bubbly example which was very helpful for me to define the design.Bubbly – CSS speech bubbles made easyleaverou.github.ioThe basic layout and design looks like the following.I also put this to codepen.io.Basic Design of the Message Board AppImplementation with Embedded RubyWe will hard-code values of the messages data, and use templating of Sinatra to show the data..The data is passed to the erb, for which this stackoverflow post was very useful to understand.Here you can check the coding..The l_main.erb and the v_message.erb should be in the views folder so that Sinatra will find them.With :locals we can pass data to the erb.2nd step: Read messages from the dbIn this step we enhance the app to read the messages from the db..For this we need to define a database and a table, and put some entries to it.Photo by Nikolay Tarashchenko on UnsplashDefine database and tableFor the next version of the app we need a database to store the data.For this step the following tutorials were very useful.How To Create, Remove & Manage Tables in PostgreSQL on a Cloud Server | DigitalOceanPostgreSQL is a database management system that uses the SQL querying language..It is a very stable and feature-rich…www.digitalocean.comPostgreSQL Ruby tutorialThis is PostgreSQL tutorial..This tutorial covers the basics of PostgreSQL programming in Ruby language.zetcode.comIn this step we will:create a new user messageboarduser in the system for accessing the databasecreate a new user messageboarduser in PostgreSQLdefine database messageboarddefine database table messageboardmessagesCreate a new user in the systemWe create a new user with the following commandsudo adduser messageboarduserYou need to provide a password..(I used messageboarduser also for the password, but of course you should not do the same in a productive environment.)Create a new user and database in PostgreSQLLog into the default PostgreSQL user (called “postgres”) to create a database and assign it to the new user:sudo su – postgrespsqlCreate a new user that matches the system user you created.. More details

Leave a Reply