So, let’s start with the Hello World application of Rails programming -> a simple blog.
First things first, I am assuming that you have Rails 5 and Ruby installed on your system.
Ready …. Set …. Go!
Open your Terminal or Command Prompt. From here on out we will just call it the Terminal.
Pick your folder that you are going to put your application in.
Now let’s start your building the application.
Go into the Terminal and type rails new blog
From there you will see your rails framework being built for your new blog application. When it is finished running and you are back to showing the cursor in your terminal change directories into your blog application.
You get there by running cd blog
Then bundle your application. This will ensure that all the dependencies of your application are installed. You do this by running bundle
Now if you want to run a check to see if your app is up and running. Run the command rails s
When it is running, go to localhost:3000 in your browser to see the splash page for the Rails application.
That’s great! We now have proof that our Rails framework is in place and our app is running.
Shut down your Rails server by going back to your terminal and hit control c -> ctrl-c
Now we want to create the actual blog application. To do so let’s think of the main parts of the app and create an outline:
We will want to have a title for our blog post. We will want to have a body which will be where we put our text, the meat of our post. We will want some tags for our post to help people easily find our blog post. These will be keywords that describe our blog post.
So we want an area for our title, body, and tags.
How do we bring this to fruition?
Head to the Terminal…. and run rails g scaffold Posts title body:text tags
Above is the example of the Posts database migration. It identifies that the title is a string object, the body is a text object, and the tags are a string object. Going back to when you ran the command in the terminal – if you identify an object and leave the classification blank it will default to a string object – so rails g scaffold title body:text tags is the same as rails g scaffold title:string body:text tags:string
Time to run your migration.
Head to the Terminal and run rake db:migrate
This will set up your database for Posts. Unless you have specified an alternative database you are using the SQLite3 database that ships with Rails. This database is not for major production applications, but it is great for testing.
Now that you have your database set up let’s set your default route to go to the Posts index page as your home page. Let’s open the routes.rb file under the config folder.
We will set the home page to the Posts index page. Add root “posts#index” under the resources posts text in the routes file.
This sets the root of your application to the Posts index view. The view is found in views/posts/index.html.erb If you make changes to this file you will see it on that page when you bring up your browser.
Time to test what you’ve coded so far. Set up your test environment.
Run rails db:migrate RAILS_ENV=test
After the migration has completed and the test environment is built, go into the Terminal and run rake test
Once you have seen that all your tests have passed, we can take a look at the app.
So, let’s see our app in action. Run rails s in the terminal and go to localhost:3000 in your browser. You should your blog posts index file. Give it a test run. Click New and write your first blog post.