Ruby on Rails – Blog Application (Rails 2 App)

Creating the Blog Application

For creating this application you must install Ruby on Rails on your system and also install the database to add the backend for your applicaton.

Click here for installing Ruby on Rails and MySQL Database.

Follow the steps for creating your blog application in Ruby on Rails :

    1. Open a terminal, navigate to a folder where you have rights to create files, and type :

      $ rails blog -d mysql

      -d mysql option create database with MySQL connectivity.

    2. Now move to blog folder using :

      $ cd blog

    3. Database Configuration

      Open the database.yml file to edit the database settings.
      For this enter following commands in terminal:

      $ cd config/
      $ gedit database.yml

      Replace username and password with your MySQL Username and password.
      Update these changes in all three sections : development, test and production.

      After saving these changes, run this command in terminal:
      $ cd ..

    4. Database Creation

      Run the following command in terminal to create database :

      $ rake db:create

    5. Starting up Web Server

      Run this command in terminal to run default webpage in browser:

      $ script/server

    6. Creating Controller

      Run this command in terminal:

      ruby script/generate controller home index

    7. Edit Home Page

      Open app/views/home/index.html.erb file. Run these commands in terminal:

      $ cd app/views/home/
      $ gedit index.html.erb

      Add following line and Save the file.(You can also delete the previous text)


      <h1>Hello Rails !</h1>

    8. Remove index.html from public folder

      $ cd
      $ cd Desktop/blog
      $ rm pulic/index.html

    9. Setting the Application Home Page

      Open routes.rb file.
      $ gedit config/routes.rb

      Find the line beginning with root :root and uncomment it.
      Replace that line with this :

      map:root :controller => ‘home’, :action => ‘index’

      Now save that file and if you navigate to http://localhost:3000 in your browser, you’ll see the home/index view.

    10. Creating a Resource

      In the case of the blog application, you can start by generating a scaffolded Post resource. To do this, enter this command in your terminal:

      $ script/generate scaffold Post name:string title:string content:text

    11. Running a Migration

      Use following command to create tables:

      $ rake db:migrate

    12. Add a Link

      Open index.html.erb file to add link to My Blog page.

      $ cd app/views/home/index.html.erb

      Add this line at last,

      <%= link_to “My Blog”, posts_path %>

      and save the file.

      The link_to method is one of Rails’ built-in view helpers. It creates a hyperlink based on text to display and where to go – in this case, to the path for posts.

    13. Test Your Blog Application

      $ script/server

      Now open http://localhost:3000/ in browser.

    By these steps your blog is created and if you find any error then feel free to ask and share it through comments.

Leave a Reply

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