I just want to give a brief overview on how one can get started building a backend that uses Sinatra with Active Record coding in Ruby. With this blog there are some assumptions that one will know the frontend CRUD to talk to the backend. Let's start with a new VS code window.
We are going to create a bunch of folders and files. For now let's go off this screen shot. Everything on here is empty so we will go into the purposes of each and fill them out as we go.
Let s start with our Rakefile.
What is happening here is the first description will start the server for us once we enter into the console and type rake server. The next two lines are designating a port in which to run the local server on. Then the next line rerun allows auto-reloading of server when files are updated. Finally the last line allows us to call rake console in the terminal to enter into Pry.
Now our Gemfile. I'll leave in the comments as there own descriptions of each gem.
Our config.ru will look like this.
Rack::Cors allows for Cross-Origin Resource Sharing request. Rack:JSONBodyParser will parse the json from the request body into params hashes.
Next we are going to go all the way up to the config/ environment.rb. We will not use the db migrate until we start building our tables.
The first line is an environment variable that is used by some of the Rake tasks to determine if our application is running locally in development, in a test environment, or in production. The next two lines require our Gems.The last line requires all files in 'app' directory.
Finally we just need to create the inheratence relationship in our Application Controller.
Alright now lets create some tables to work with. Open your terminal and cd into Server. Run "bundle install". That will install all of our gems. Now we can create our tables. We will create a table called "humans". Type "rake db:create_migration NAME=humans". Once you hit enter you will see that the terminal will display that there is a new file in the migrate folder. You will also see it in the explorer with the timestamp that it was created.
Remember that we need to be very specific with our naming conventions. Models and Classes are singular. Tables and schema are plural.
Let's create another table called "dogs". Then we will create a relationship between the two and build the tables. Type "rake db:create_migration NAME=dogs" into the teminal. Alright now we have two tables to work with.
In our humans migrate file we will create a table using the "create_table method. It will look like the following to give us three columns. The id will be the first column and that will be auto generated. The second will be their first name and the last will be their last_name.
Now lets work on our dogs. It will look very similar but we will add a foriegn key to establish a relationsip between a dog and their human.
Now we can run "rake db:migrate" to create our SQLite tables.
We can now see the schema that was created by active record as well.
In app/models we build the classes for dogs and humans. Remember their classes will be singular.
Our classes will look like the following inheriting from ActiveRecord::Base.
Now we have the base for our Sinatra backend. Next blog we will go over how to create instances of the classes to populate data. Also how to create a seed file to add data. Then how to use the Application Controller to allow for CRUD.
Top comments (0)