Creating A Rails Project, Setting It Up in Github: The Basics

I’m currently in my third week of mod 2 on Flatiron’s software engineering course and if you’ve gone through the bootcamp process at Flatiron or currently are, you know exactly which point I’m at — Rails. I was introduced to Rails a couple of weeks ago and this week, I’m building my first Rails project. Naturally, I had a lot of trepidation building a project from scratch. At the end of mod 1, under three weeks ago, I had just gotten used to navigating three files (four at most) in VS code (classes and seed data) and in the last two and a half weeks, I’ve been getting to grips with navigating a lot more….not going to lie, it was hard to get my head around.

Well, I’m here to agree with you that, yes, it looks crazy and, yes, I also asked myself, repeatedly, “Where do I even start?”…also if you prayed to a deity for guidance, don’t feel ashamed cause so did I..

I’m here to show you that fear not, you can do this. If both my brain cells can get round this then so can you.

At the point you’re sat in front of your computer ready to create your project and files, I trust that you’ve decided on your project and at the very least have a name (I’ll also like to add at this point that I won’t be using scaffold, I’ll be showing you how to do this the basic…well, fairly basic way).

To walk you through this, I’ll create a very basic project called “Picnic” and will walk you through the setup in VS code and Git Hub.

I’ve opened up VS code and in terminal, I’ve ensured that I’m in the right folder. I have a folder called “mod2 Rails Project” and I’ve switched to that directory. In terminal, I’ve typed in:

This will create and install a bunch of files. For the next few seconds (or minutes…depending on the age of your computer), your terminal may resemble the matrix. Fear not, those are all the files and folders necessary to your new project. Once this is done, your terminal may look like this -

In my terminal, I’ve opened the Picnic project by typing -

cd stands for change directory. I’ve also opened it up in VS code by typing “code .” in my terminal.

On the left, will be a bunch of folders. Fear not because you won’t need to access all of these.

I’ve decided that my project will have two classes — “Snacks” and “Guests”. I’ll only need to access the “app” and “config” directories. In the “app” directory, I’ll need to create controllers, models and views. In the “config” directory, in “routes.rb”, I’ll need to add my routes.

Now, let’s get started.

First, I need to create my controllers. In terminal, I’ve typed in -

g stands for generate.

I’ve typed in “snacks” and not “snack”. The controller must always be pluralized. If you’ve made a mistake, don’t worry, just type into your terminal –

d stands for delete

This will reverse what you’ve just done and you can regenerate the files with the right spelling.

This also creates folders for each in the “views” directory. The folders in the view directory will also be pluralized. As you know, your controller and views work in tandem with each other.

Next, I’ll create the models –

Your models must be singular. I’ve typed in “snack” not “snacks”.

In app -> models is a file called “snack.rb”. Now what happens if I misspelled “snack” and typed in “snacks”. Fear not, again, you can reverse this by typing in -

I can now recreate the file using the right spelling.

My project is now taking shape, I have my controllers (in plural), my models (in singular) and my views (also in plural). Now, I need to create some routes, you really don’t need to do this till you start coding but I include it in my setup process.

In config -> routes.rb, within this method, Rails.application.routes.draw do, type in your paths. There’s many ways you can do this, you can type in –

This creates all seven RESTful routes per model. This is mostly frowned upon because if you don’t need all seven routes, you’ll have pointless code in your project. For my project, I’ll need index pages so, I’ll amend mine to –

At any point to check if your routes work, type in “rails routes” into your terminal and it’ll show you which routes are setup.

Now, because I’ve only created the index route for the guests controller, in the controller file, I’ll create an action for index. So in app -> controllers -> guests_controller.rb, I’ll add an index action.

Now, I’ll need to create a html file for my index action in views. So in app -> views -> guests, I’ve right-clicked on “guests” and clicked on “New file”. I’ve called this file “index.html.erb”.

I’ve repeated the same process for “snacks”, in both snacks_controller.rb and in the snacks folder, in the “views” directory.

Et, voila!

Your Rails project is now setup! See, wasn’t that hard!

Now, Github.

In your project, in VS code, notice how all your files are in green with “U” next to some of them. Also, notice at the bottom left of your VS code is “master *”

Open up Github, got to your repositories and click on “New”, in “Repository name *”, in that field, I’ve typed in “Picnic” (leaving a description is optional).

Now, you can choose to have your repository public or private. If you choose a private repository, you have to pay for some features which you’d otherwise get for free if you chose a public repository. The discussion in this link, explains the difference in greater detail.

I’ve selected “private” and can now click on “Create repository”. As I’ve created my project, I only need to focus on the steps under:

First, I’ve copied and pasted the line below into my terminal.

Still in my terminal, I’ve typed in -

Followed by -

Then go back to github and under “…or push an existing repository from the command line”, I’ve copied and pasted the second line into my terminal -

Now you’ll notice that all the folders on the left are now white and the bottom left “master *” is now just “master”.

Now, I don’t want to work on my master branch. I want to work on a development branch so, in terminal, I’ve typed in -

This creates a new branch called development.

Now, I won’t be working on this project alone. I’ll be working with collaborators. To add them, I’ve followed these steps -

Back in Github, at the top of you page is “Settings”. Click on Settings -> Manage Access -> Invite a collaborator. Then add the collaborator’s Github id.

Your collaborator will receive an email which they’ll need to accept.

When working on a project with multiple collaborators, it’s advised to never work on the master branch nor the development branch. For the next stage of the process to work for you it’s best that you and your collaborators are working on different aspects of your project. If you’re all working on for example “guest.rb”, then this next stage will lead to issues for you. So, please ensure that you’re all working on different files within your project.

Before you start working individually on the project, you’ll need to each create a local branch and when you’re finished working on your local branch, you’ll need to merge your changes to the development branch and push into github. Your development branch will be the main branch you will allow access at the start of each day to work on your project.

In your open project in VS code, in the terminal, type in –

This takes you to the development branch. Then type -

This pulls the current development branch to your local machine. Then type -

This creates a new branch and I’ve called this new branch “index”. This is the branch I’ll work on. My collaborator will have replicated the above process and will be working on a local branch as well.

It’s important to update your project in Github at least daily. When you’re done for the day, do the following steps –

This adds all your changes then type -

Your message should pertain to the changes you’ve made so your collaborator(s) can easily identify them. Now, type in -

This switches to the development branch. Type in -

This pushes your local branch to Github. You can now type in -

and then type in -

This merges the local branch, “index” with the development branch, updating the development branch with my changes.

Pushes the updated development branch back to Github.

Noticeboard: When working with multiple collaborators, please ensure that you’re not merging with the development branch at the same time. Always notify your collaborator before you proceed with the steps above. You will have to do it individually. If you all do it at the same time…

Et voila, you have now setup your Rails project in Github.

Junior Front-end Developer