How to develop a blog using Ruby on Rails development

By -

Brief Overview of Ruby on Rails

For web application development, Ruby language is fast catching language, and Ruby on Rails is a framework for the language. It is immensely helpful because the framework gives you the flexibility so that you don’t have to build database access layer every time. Also the main function of the framework is to solve clients’ issues rather than entangle in implementation and functioning of MVC pattern on a regular basis.

A brief sketch on MVC

MVC stands for Model-View-Controller, which is an advanced programing subject, so if you don’t know the subject, it is better not to think about implementation, rather focus on how to make use of it.

Model layer is for define class for the data application. For example, for developing blog, if you want to store comment, you will have a “Comment” model. The model has the ability to communicate with the database, store and retrieve data.

View layer is principally meant to return the relevant HTML to be shown on the user’s browser. In Rail it is viewed as erb (Embedded Ruby) file, which is amalgamation of HTML and Ruby statements.

Controller main function is to interact with the model to retrieve and store data.

How to create a new Rails project in blog

The first thing is to create a new project. In Rails, all the files are stored in a folder called project. The command to create a project provided you are are using MySQL as the DBMS is

> rails myblog -d mysql

Application Design

The important point to create a Rails is to understand how you constituent the data structure and relate to each other. The design structure is various, and for designing a blog application, first you need to chalk down things on paper what and how your application works. After completing the pen and paper work, highlight the nouns that are required to store the information to the database.

For example, “ my web application is blog, allowing me to create posts and allow users to comments.”

So, the model works here for Posts and Comments (we omit the people/user for sake of simplicity).

The next step of designing is to understand how your model is relate to each other. The most common relationships in Rails are:

  • One to One
  • One to many
  • Many to many

You need to clear about the relationship between models. For example, a comment can be infinite for one post but it must belong to one post. This is an example of one to many relationship.

Scaffolding

Scaffolding is important as it creates mock up for your clients. This is also a practice for acquiring skills in Rails.

To create applicable scaffold files for posts, you can generate them with a single comment:

> ruby script/generate scaffold post title:string body:text

This command creates variety of files such as controller.rb, post.rb, edit.html.erb, show.html.erb etc.

You can also create comments by scaffolding by running the command such as

> ruby script/generate scaffold comment name:string body:text post:references

Creating database and migration

After creating the model, you need to create the database so that it can store the information about posts and comments and also retrieve when required.

You need to add the username and password into database.yml file such as

development:

adapter: mysql

encoding: utf8

reconnect: false

database: myblog_development

pool: 5

username: root

password: yourrootpassword

host: localhost

Database migration means to move your stored data from one state to another. For example, creating tables, removing new fields, and adding new fields.

 

+Kerry Tiwana is a full time tech savvy blogger, author and founder of TechMafia, you can follow him on Facebook.

Leave a Reply

Your email address will not be published. Required fields are marked *