Your first Rails 5 Application – the Blog

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

bandicam 2017-04-05 21-02-00-380

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

bandicam 2017-04-05 21-07-40-112

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.

bandicam 2017-04-05 21-09-06-290

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

bandicam 2017-04-05 21-46-35-347

So let’s explain what we just did. Now, there are many ways to do what we just did. This method we used the scaffold. We generated the entire scaffold available for the Posts. This method created a Posts controller, Posts Model, Posts Views, Posts Tests, Posts Helpers, and Posts Assets Files for javascript/coffescript and CSS. We also set up the Posts routes, and database migrations.

bandicam 2017-04-06 20-16-13-608

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

bandicam 2017-04-06 20-42-09-618

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.

bandicam 2017-04-06 20-52-29-196

We will set the home page to the Posts index page. Add root “posts#index” under the resources posts text in the routes file.

bandicam 2017-04-06 20-56-56-989

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

bandicam 2017-04-06 21-09-16-239

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.

bandicam 2017-04-06 21-05-19-465bandicam 2017-04-06 21-05-51-926bandicam 2017-04-06 21-06-06-889

Congrats!! You have created your first Rails Application.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Powered by WordPress.com.

Up ↑

%d bloggers like this: