For this post we will be creating a small Ruby on Rails web application which can act as a school website. It will hold Professors, courses, and sections of those courses. It will need to have a database which will hold a persistent copy of the information which we will enter. For this we will use the standard SQLite3 database which comes with Rails. Also note; If you want to look at the code for this project, you can do so on ~GitHub here~ (code gone, tough cookie kid).
Before we can get started, we will need to first install both Ruby, and Rails. This process will vary depending which type of operating system you are using. Since I am using Ubuntu (a Linux distribution), so I will show this process. Most Linux distributions should come with a copy of Ruby preinstalled. You can check if your system has Ruby by opening up a terminal and issuing the command:
$ ruby -v
If it is installed, you should see the command return a version number;something like this:
$ ruby 2.3.3p222 (2016-11-21) [x86_64-linux-gnu]
Next we will also do the same for Rails. Type in the following command:
$ rails -v
If it is installed, it will return something like this:
$ Rails 5.1.4
Otherwise, we can install Rails by issuing the command:
$ gem install rails
Gem is our package manager for Ruby. Anytime we need to use external libraries, we can use gem to go fetch them to include in our project. If this command fails for some reason there are plenty of resources available online to help you install Rails. For this sake of this post I wont go into the details of troubleshooting a Rails installation. Another thing to note is that using an IDE (Integrated Development Environment) when working with Ruby can be very helpful. I have used RubyMine and would recommend it; as it helps figure out a lot of the initial setup of your environment for you. If that’s not your style there is always Emacs.
Now that we have our environment set up we can begin to work. But before we jump straight into writing our Ruby code, the next thing we will need to do is figure out the relationship between our different pieces of data. For the sake of simplicity we will say that the only information which we will require right now is that a Professor has a name and many sections. A section has a number and a course. And a course has a name and can have many sections. The models might look something like this laid out.
professor model: * professorName:string * section:reference
section model: * sectionNum:integer * course:references
course model: * courseName: string
Since we now know how the pieces are related to each other we can begin the code writing process. Go ahead and navigate to where you want to create your new project and issue these two commands in your terminal.
$ rails new myapp $ cd myapp
This creates a barebones empty Rails project for us to begin working on. And we have changed into the new project directory which was created for us. Next we will we generate our models which our application uses. Go ahead and issue these three commands while in the myapp directory.
$ rails generate scaffold Professor professorname:string section:references $ rails generate scaffold Section sectionnum:integer course:reference $ rails generate scaffold Course coursename:string
By issuing these three commands Rails automatically created our models with their data types and the corresponding views and controllers for these classes. The next step we need to take is to perform a database migration. This instantiates our sqlite database and migrates these changes into our project. While still in the myapp directory issue this command.
$ rails db:migrate
We can now start up the server and take a look at our application as it sits so far. To do so, we simply type this command to start up our Puma web server.
$ rails server
Navigate your web browser to the address http://localhost:3000/ and you should see a default Rails welcome page. Before we make any further changes to our project, now is a good time to start tracking our changes in git. Git should have been pulled in when you created a new project so you can simply execute this command to add you files to track.
$ git add .
Now we will need to make some changes to the code to make it a bit easier to view and edit our information. Most of these changes are in the app directory. ~Here are the differences for the file changes which I have made~ (changes lost). Consider it an excercise to figure it out yourself ;)
Take a look at each page. While your Puma server is still running, navigate to the page http://localhost:3000/courses and you should see a page allowing you to add Courses. Here we can create a new Course and assign it a string as a name. If we go to http://localhost:3000/sections We can create a section of a particular course and assign it a section number. Lastly, If we go to http://localhost:3000/professors we can now assign a professor to a particular section. All our basic functionality is there!
By following the Rails way of doing things we were able to get a small web application off the ground without (much) configuration. We can call this approach convention over configuration. There is obviously a lot more which we could add to this example, but this was just to show how to get up and running. In the future we will be adding more functionality to the site and other things like testing and error handling. I book I would recommend you check out for further reading is Agile Web Development with Rails 6 by Sam Ruby.
[Originally Posted 09/2017]