I have had this idea for my profile for some time. It is ever changing but the central elements are always there. Today let’s start this thing and see what we can do. I am excited to play with Rails 8, how bout you? So what do we need to do here?
I need a place on my computer where I can create different Rails apps for different versions.
I need to create a new Rails 8 app for the MILK project and make sure it works out of the box.
I need to create a Github repo for MILK on Rails 8 and push it up.
I need to create the first issue on the repo that addresses the fact that MILK doesn’t have a home page.
I need to create a new branch from that issue and switch to it on my local system.
I need to create the home page, on the new branch and change the route to the root route, then verify that it works.
I need to create a test to ensure the page elements are there and correct so I can verify the page works with code.
I need to push the new branch to the Github repo, merge the branch to main and delete the old branch from my Github and my local system.
I think that is plenty to get us started, what ya think. The morning is ours, let’s go get some! Oh, speaking of, I forgot my coffee, oh and sweet tea. Good grief I’m a mess, half a moment please. OK, you ready, I’m ready. RAILS 8!
Various Rails versions on my system.
First thing, I need to get rails 8 on my system because I am going to be playing with it a lot. I don’t want to have to upgrade every application I create, would just rather throw a “rails new project_name” in the command line and have it just do it’s thing.
I run PoP OS, which a Linux operating system, on my laptop. I love Linux. I have put several different Linux operating systems on this beauty. As for Rails, of course you need Ruby, and for Rails 8 you need Ruby 3.2.0 minimum. I use the RBENV Ruby version manager. The most common version managers for Ruby are probably RVM and RBENV. There are some differences so if you are new, please do some quick research and pick the one for you. Also, if your on Linux, it is a great idea to ensure the system is up to date and upgraded before installing anything. Those commands will be a bit different depending which base OS your built on. PoP is built on Ubuntu so I sling that “sudo apt” around.
For me, I decided to create a directory, call it “rails 7”, changed directories into it and ran “gem install rails 7.2.2”. Once that’s completed I ran “rails -v” to see the current version. Now any time I create a new project in the rails 7 folder it will be with Rails 7.2.2. I will do the same thing for Rails 8.0.0. This way if I want to spin up a quick project in 6, 7 or 8, I can do just that with minimal effort. I will let Deadpool use maximum.
Quick Rails 8 project build
Now, in the “rails 8” folder I will sling “rails new milk” in the command line and wait a moment while Rails builds the new project. Once I have my prompt back I can “cd” or change directory, into the milk folder and run “bin/dev”. As the server is being spun up I can navigate to localhoast:3000 in the browser. Look it there! The new, shinny Rails 8 app running with Ruby 3. Super sweet. I bet you thought that was going to be more than just a paragraph long.
Well, you were right, but I could have stopped there and moved on. However, there is one small thing I want to address quick before grabbing that Github repo. Code today is modular. That makes it awesome. For the JavaScript world you can import modules after grabbing them with npm. Rails is just the same, only those modules are called gems. When you set up a project with React or Next you will have a “package.json” that will list out all the packages or modules that are used to make the app work. Same thing here with Rails except that the list is called the “gem file” that lists out all the gems.
Now I have explained all this because with Rails, those gems are kept in a folder called “bundle” that is located in the vendor folder. If we don’t add a line to the gitignore file then git will see those and push them with the everything else. This annoys me because they are not needed for this operation. Rails needs them to work right and if you were to grab my code, one of the first things you will do is run bundle. Running bundle will run through the gem file and make sure the app has everything it needs. It will see that none of those gems are there and will grab them to sweet, no fuss. So why do I need to push that weight?
# ignore bundled files in vendor folder
# all these files will be repopulated on running bundle
/vendor/bundle/*
There is the code I added to the gitignore file. It tells git to look in the vendor folder for the bundle folder and that asterisks just says everything. In the terminal or command line I just opened VSCode with “code .” and modified the file. If you have Nano or VIM or if you just want to use a text editor real quick, up to you. You can even skip this addition to the gitignore file and just move on. All good here.
Setting up the Github repo
If you don’t have a Github account, why? There are many git tools for you today. I mainly use Github because I believe it is the most common and most familiar, but I have also had a Gitlab account. There is Bitbucket and code storage on AWS and Azure. Pick your poison. This will be stored in Github.
Let’s bounce over to Github and click on repositories then that green “new” button in the top right to create a new repository. There is a lot of stuff on this page. The only thing I am interested in is the name field. I will call mine “milk-rails8” because I already have a milk repo. That is it. Rails already comes set up with git, I just want to have a repo on Github. So the project already has a readme and a gitignore file. So after I fill out just the name section and nothing else, I will quickly glance at the name it suggest for a giggle. This time it was “shiny-octo-broccoli” and I will click the “create repository” button at the bottom of the page.
Yes, this is what I am looking for. Here you are presented with two options. The first one is instructions for setting up a new local git repo. Don’t need that as Rails has already done that for me. The second option is the instructions for pushing an existing repository up to the new Github repo. That is what I am looking for. I have a local repo, just need to create that remote branch so I can store my code in the git cloud.
git remote add origin git@github.com:Developer3027/projectname.git
git branch -M main
git push -u origin main
Here is the code I am talking about. If you have not set up ssh in your Github account, I would recommend doing so. It is a secure way to interact with the repo so when I run commands against it, Github will verify a ssh key before getting down to it. OK, I will make sure I am in the milk folder in the rails 8 folder on my local system and paste in that code I copied from the second option from the Github instructions. Tapping that enter key with authority will set the Github remote branch to that url, make sure there is a main branch that all the code is on, and push it up to that remote branch on the cloud.
Hang on. What kind of snake oil am I selling? You probably got a error about failing to push some refs and that the src refspec main doesn’t exist. Welcome to code and being a developer. You will get errors all the time. Learning how to deal with them is a huge part of development. This one is simple enough. If you read the error, in a round about way, it is telling me that it can’t push. After setting up the app, we never did add anything to the repo or commit. Let’s talk about git for a second.
git status
git add .
git commit -m ''
git push origin main
You run “git status” to figure out what the status is. Lines in green are added or tracked. Lines in red are not tracked or have been changed. Running “git add .” will add all the files, or you can use “git add file-name” to add just a file at a time. Once your work is added then you can commit them to git with a message that describes the work done using the “git commit -m ‘message’”. I just used “initial commit” for this one. In general you want to be pretty descriptive. Then you can run “git push origin main” and seeing as the remote was set up before the error, the code will now be pushed. I used to run “git status” after every step to see what had happened. Now what happens when you refresh the browser? Sweet biscuits there’s code.
Create the first issue
We have the app built and it runs but there really isn’t anything there. Let’s make a home page for it. When I say home page I mean the main page of the site that everyone will first visit. The root of the app. The site index page. For Rails this is pretty straight forward, and it will provide a great introduction to how I use some of the functions of git.
In the Github repo, click the issues link located in the navigation along the top of the page. Click the green “new” button and fill out the form. For the title I will put “No Root” and I will provide a description. In general you want to provide some descriptive information in the description. Good information for you to know; the title will become the name of the branch you create. You will kick yourself later if you just put “description” in the description and you are trying to remember what all you wanted to accomplish in this issue, so write it like you will get back to it. Github uses Markdown so you can style your issue if you like. Once done click the green “submit new issue” button.
Once the issue is created, on the right you will see several options. You can assign the issue to a developer, add labels, projects or milestones. Under development you will see a link to create a branch from the issue. It is always a great idea to work from a branch and never push directly to the main branch. Now of course you can, we just did, but the main branch should be considered sacred and production code, so don’t mess with it. If you are working from a branch then you can totally destroy that code and it will only effect the main branch after going through several checks and surviving testing. Not likely. We will see this in action by the end. In development, massive projects are built one feature at a time. Facebook, YouTube, whatever was all built one line at a time, one feature at a time. Write a block that works, test it, push it and on to the next. So click that link and create the branch.
For me I will work on the branch locally so I will leave the defaults and click the “create branch” button. Thank you Github for that. Copy the code block and paste it into your terminal. With the run of the command you are now on the new branch and ready to create the home page.
Create the home page
Now let’s use the Rails cli ( Command Line Interface ) to create the home page. Let’s go over a few things first. Rails works off the MVC ( Model View Controller ) methodology where the model will relate to the database, the view is whats shown in the browser, and the controller handles the logic. This home page is going to be a static page, meaning that I only want to display information and not much more.
In Rails a view is driven by the controller, even if there is no logic there to evaluate, Rails will first ask the controller for guidance, then find the view and present it. So we need to create the controller and the view. In the cli you can pass the -p flag at the end for pretend. If you ever want to know what Rails will do, you can pass the pretend flag after the command and review the output. If it looks good, remove the flag and run the command again.
bin/rails g controller pages home -p
There are a few things here that may be a little different than what you see in tutorials. First is the “bin/rails”. Starting in Rails 4, Rails included a bin folder that has some cool stuff in it but the reason I use it is because using it relates specifically to this project. Effectively I am running Rails, but only the version specific to the project, referring only to the project environment. “g” is a short cut for “generate”. The controller name is “pages” and one of those pages will be named “home”. Here is the output for the command.
create app/controllers/pages_controller.rb
route get "pages/home"
invoke erb
create app/views/pages
create app/views/pages/home.html.erb
invoke test_unit
create test/controllers/pages_controller_test.rb
invoke helper
create app/helpers/pages_helper.rb
invoke test_unit
So it will create the “pages_controller.rb” file, set a route, create the pages folder in the views folder and create the “home.html.erb” file in it. Then it will create a test and helper. Looks good, so I will press the up arrow to get back to the last command I wrote in the terminal, delete the pretend flag and rerun the command.
If you have not opened your IDE ( Integrated Development Environment ) yet, then it is time to do that. I use VSCode as my editor of choice but there are many to choose from. I am considering installing VIM because working straight from the command line “just feels right”. I really like the functionality I get in VSCode though. Anyway, let’s open up the routes file in the config folder and change that route to the root route.
In the above image you can see that I just commented out the route the command wrote and changed the commented out root path. For the root path I want to look in the “pages” controller and show the “home” view or run the home action. Notice that the root path uses the “#” hash symbol where the get route used the “/” forward slash. Save that file and refresh the browser. You now see the home page!
Test the home page
When I started this article I wanted to dive into writing a nice test that tested several elements on the home page. However, you may have noticed that the initial push to our Github caused one of the test to fail. Error error every where. That’s ok, just one and it is because of the database. In short, Github doesn’t see the database, so it can’t run one of the tests. Fixing that is more involved than I want to get into with a simple project setup, but don’t think that we get out of writing a test.
bin/rails test
Remember just a moment ago when we created the home page? That command created a test for the controller. That test grabbed the route and checked that the response is a success. If you run that code block to run the Rails test, what do you get. Fail! Big fat error. It was not that Rails wrote a bad test but rather we changed a condition that broke it. We set the root to the home page and removed that route to “pages_home_url”. Open the file “pages_controller_test.rb” in the test/controllers folder and change the get - “pages_home_url” to get - “root_url”. Test it again and you should get a pass with 1 run and 1 assertion.
Finish up the branch
Great! We have done a lot of work here. Nothing special but all good work. Let’s wrap this up strong. Do you remember how to check your git status?
git status
Looks like I modified the routes file and have created 4 files that are not tracked. Go ahead and add all the files. Make a commit message and push to the working branch. If you look at the output of the status command, the first line is the branch you are on.
git add .
git commit -m 'Created the home page and pages controller. Added root route. Modified test for home and test passed.'
git push origin 1-no-root
Once the code is pushed head back to the Github repo and if you are not on the home page, click on the code link at the top left. You should see a notification that there has been a recent push to the branch we were working on. Click that “compare and pull request” button. Click the “create pull request” button. Now Github is going to run several test to see if it is ok to merge the code we just wrote with the main branch. Remember we had a test fail earlier when we created the project because Github can’t find the database. Once again we will see the test fail here. If you want to know exactly what failed you can click the details link on the right. I encourage you to take a look, but we tested it local and the tests passed. I am not going to fix this issue for this article. I am going to go ahead and merge the branches by clicking the merge button.
Once merged it will ask if I want to delete the branch. Yes. That task is complete and the work is done. I don’t want the branch hanging around so go ahead and delete it. Now that Github is wrapped up, we need to wrap up the local git by deleting the working branch.
In my terminal I will checkout to the main branch and then pull against the remote branches main to ensure I have the latest code. The code on my local main branch does not include the home page I just created. It will after a pull the code down. Now that I have the latest and greatest I need to delete the local working branch because I now longer need it taking up space.
mason: git checkout main
Switched to branch 'main'
mason: git pull origin main
remote: Enumerating objects: 1, done.
remote: Counting objects: 100% (1/1), done.
remote: Total 1 (delta 0), reused 0 (delta 0), pack-reused 0 (from 0)
Unpacking objects: 100% (1/1), 945 bytes | 945.00 KiB/s, done.
From github.com:Developer3027/milk-rails8
* branch main -> FETCH_HEAD
af615 6..05d d0b main -> origin/main
Updating af615 6..05d d0b
Fast-forward
app/controllers/pages_controller.rb | 4 ++++
app/helpers/pages_helper.rb | 2 ++
app/views/pages/home.html.erb | 2 ++
config/routes.rb | 3 ++-
test/controllers/pages_controller_test.rb | 8 ++++++++
5 files changed, 18 insertions(+), 1 deletion(-)
create mode 100644 app/controllers/pages_controller.rb
create mode 100644 app/helpers/pages_helper.rb
create mode 100644 app/views/pages/home.html.erb
create mode 100644 test/controllers/pages_controller_test.rb
mason: git branch -d 1-no-root
Deleted branch 1-no-root (was 3a9e 6d).
mason:
Sweet Biscuits we are at the end! I hope you have enjoyed this little walk through of the initial set up of MILK on Rails 8. I often see tutorials that give great information but miss certain systems to make things quick, I guess. Maybe I am missing something but to be a real word developer you need to be able to mix all these skills together. Not really a quick thing, but for the freshers, it can be confusing. I did not have to do all I did here just to get the app up, but this is exactly what I have to have done by lunch if the team is going to get anything done.
I hope you have enjoyed. Learned a little something. Had a good laugh. Scratched your head or jumped up pumping your fist at least once, maybe banging you knee on the table and spinning around, hand on the pain, to notice everyone looking at you through the tears that are forming, and you still throw that hand high, letting out a Micheal Jackson howl in victory. ( maybe just a dream of mine, still, look into a standing desk. )