Sinatra Web Application using ActiveRecord and SQLite

Michael Werling
3 min readApr 18, 2021

After my second month at the Flatiron School, I had the pleasure of creating a web application using the Sinatra framework. This opportunity allowed my to use all of the Ruby skills that I learned in the first month of the course and build off of that with cool new tools like ActiveRecord and SQLite, which allow data to be stored in a database.

The basis for my project was a way to track inventions that spontaneously pop into your head. You can store the ideas in this app and go back to them if you need to update information on them or delete them if they are no longer viable. For more information on how the app works, please see the video below.

Getting Started

Our instructor gave us the ability to use the corneal gem to set up the overall structure of our project. This came in handy as it included a good overall layout and structure to the application. This setup also provided a list of additional gems to include in the overall functionality of the website.

├── config.ru
├── Gemfile
├── Gemfile.lock
├── Rakefile
├── README
├── app
│ ├── controllers
│ │ └── application_controller.rb
│ ├── models
│ └── views
│ ├── layout.erb
│ └── welcome.erb
├── config
│ ├── initializers
│ └── environment.rb
├── db
│ └── migrate
├── lib
│ └── .gitkeep
└── public
| ├── images
| ├── javascripts
| └── stylesheets
| └── main.css
└── spec
├── application_controller_spec.rb
└── spec_helper.rb

Introduction to MVC and Active Record

MVC (Models-Views-Controller) is a way to organize your overall program. MODELS are where information is stored for a given program and VIEWS are the pages and forms that the user sees that can interact with those models. CONTROLLERS are the bits of code that allow the views to communicate with each model.

The first step was to create the models that were used to store and alter the information on the application. Luckily, Active Record has a way of creating tables internally that can store all of the information that an application needs to run. By creating a migration file and running it through ActiveRecord, I was able to create a table that contained all of the information on each user that signs into the app and each invention that they submit.

Routes and HTTP

Now that I had the initial databases set up, I had to figure out how signing up and logging in to the site would be possible. I needed to create different forms for the user to fill out and had to route those forms to different parts of the domain. This took some trial and error in terms of getting the right HTTP request method(get, post, patch, or delete) and the right route destination, but with enough “practice” I was able to get everything to the right place.

Eventually, I had all of the systems working and each invention was able to be saved, viewed, edited and deleted without issue. I also had to associate each user with each invention in order to keep other people from editing another user’s work.

For Another Day

I plan on adding to this application in the near future. I originally planned to add the ability for a user to track each invention by industry segment or patent status, but did not have the time to implement these enhancements. I also want to explore more with HTML and CSS to make the format a little more friendly to the user.

Stay tuned for updates on this application and my overall coding progress.

--

--