Build a Job Listing App in Less than 10 lines
Welcome to the amazing world of Ruby on Rails - feel free to learn more about what is it on the official website, let's get straight into action... everything about MVC, TDD you can learn later or in your own time, going through tutorials like the famous Michael Hartl Rails Tutorial, but for now:
This is a screw it, let's do it approach. Rails is fun, rails is easy. Let me prove to you how easy it is, in less than 20 minutes, in less than 10 lines of code.
What tools do you need:
- Terminal App ( Terminal on Mac and Ubuntu, Command Line on Windows )
- Code Editor ( Sublime Text 2, Atom, Vim )
- Ruby - ( https://www.ruby-lang.org/en/downloads/ or http://rvm.io/ )
If you are on a Windows OS you might consider some online IDE like https://c9.io/ , tutorials on setting up a rails enviroment are largely availible within their community
Line 1 - Install rails
In your terminal just type
gem install rails
That's as simple as it gets. You just installed one of the most powerful frameworks in one line. If you want to read more on it, just refer to the official site and read what it really stands for - http://rubyonrails.org/doctrine/
Line 2 - Create a new project
Inside your terminal type:
rails new easy-jobs
As the name suggests, you are initiating a new project, it will breeze through by creating a bunch of files and folders, setting up all the necessaries for you not to waste more time.
If you want to check what it created, just open the folder
/easy-jobs with your Code Editor and see how neatly everything is organized. If you want to make sure that this works, type in your terminal
cd easy-jobs to go to the directory and type
rails server to start the server. If you visit
http://localhost:3000 you will find a fancy rails page that looks like this.
Whenever you finish admiring it, just stop the server with
Cmd+C or Ctrl+C
Line 3 - Create a Job posting scaffold
Within the folder of the app, type this long command in your terminal:
rails generate scaffold Job company_name:string job_title:string job_description:text salary:integer location:string contact_email:string
This makes a lot of important decisions by creating a database migration (for us to run in the next step), a Model, a Controller and some Views. This is what we call Model View Controller Framework, or known as MVC- read on that later, but see it in action here. Browsing through your
/app directory you find all of these in a simple-to-understand layout.
Jump into the
controllers/job_controller.rb and see if your code editor shows something similar to a class, with a bunch of methods, named conveniently for us as
index, show, new, edit, create, update, destroy, and a bunch of others...
For each of these methods, we have in our
views/jobs a html file with some ruby embedded into it, named as
.html.erb. Let's see them in action in a few steps, but before that we need to make our database understand that we have some models for it to connect to.
Line 4 - Migrate your database
One of the files created by our previous step was the one that specified which fields do our Job postings have, and what types are they. For example Salary is an
integer type of value, location is
string, and so on...
We need to prepare the database for it to receive those types, running this in terminal will set it up for us:
This should return you something like
CreateJobs migrated, so your app knows it has a database with a table, with some fields to manipulate. You are almost ready to launch.
Line 5 - Set your roots
config/routes.rb you see one line called
resources :jobs and a bunch of commented ones, if we just add another line to this file:
We will set the root path of our directory to always start at the index of our jobs controller, meaning it will go to
JobController method index, and its view in
Want to see if it worked, type in
rails server in your terminal again and just go to
Something like this should appear before you:
Step 6 - Play around
This is not even a line for you to write.
Lets test our application so far. Try creating a new job listing, edit it afterwards, delete it, try again... notice where it takes you, see how similar the path names are to your
JobController see how the views seemed to be all connected with the files you have in your
app/views/jobs, just play around.
Line 7 - Change your views
After a bit of harmless fun, you might think this is not ready to compete with linkedIn jobs section. Lets take care of that.
Changing what we see is easy, every view has some HTML and some ruby, if we go to our
app/views/jobs/show.html we can easily replace one of the section with a little more logic.
Lets try it on this block:
<p> <strong>Contact email:</strong> <%= @job.contact_email %> </p>
What happens if instead of having a text output, we could have a link that starts the application process for this specific job for us:
<p> <a href="mailto:<%= @job.contact_email %>?subject=Application for <%= @job.job_title %>">Apply</a> </p>
You can see that we changed our paragraph to a link, that goes to our mail client, and even passes the Job title into the subject line. Reload your application and you should see a fancy link to make your customers application process smoother.
Line 8 - Use it as an API
You might think, we had enough, but Rails has more to show you.
Whenever you think about taking your Job platform a step further, you might consider giving this data to Mobile Apps, or other front-end frameworks.
Well Rails already prepares you for that, in your JobController, you have some comments before your methods like:
# GET /jobs # GET /jobs.json
This means that we already have all the JSON tools that we need, to start building and using it as an API.
Have a look on:
localhost:3000/jobs.json or even on one of your jobs
localhost:3000/jobs/1.json. Your mobile team will be thankful for already giving them an API that even they can POST and PUT and DELETE and UPDATE, if you are unfamiliar with HTTP Requests, dont worry, we can look at it later.
Line 9 - Make it yours, make it better
Look how far we have come, 9 lines ago we didnt know anything about Rails, we have a full working app, awaiting our future customers. You may think, we need to add some color to it, and maybe some more functionality.
I think the colors, CSS and HTML I can leave with you, but lets see more functionality in the coming tutorials, let's add user registration, profiles of job seekers and companies, integration with emails and maybe even stripe payments for the companies to promote their jobs to the top of the listing page. All ideas are valid, if they make us happy, and if they are as easy to implement as this tutorial.
If you think you have mastered these lines, you can try your hand again at a simple link sharing site, even a small blogging platform, it still can be done by changing the main structure, but the main parts of rails will remain. Let me know what you come up with, and if you have any suggestion for the next set of tutorials, just comment or contact me below.