• home
  • forum
  • my
  • kt
  • download
  • Developing your First Ruby on Rails 2 Application

    Author: 2008-08-29 09:33:39 From:

    Like I said in my previous post, RoR uses the MVC Architecture. (M-Model, V-Views, C-Controller)

    The below diagram must be self-explainatory, but if it is not, then
    Models - Models communicate with the database, to retrieve the data and keep.
    Controller - Any requests made by the browser are forwarded to the Controller by the View, which refers the Models and modifies it in different ways (methods written in the Controller) and returns the results to View.
    View - This communicates with the browser, takes the inputs and forwards it to the Controller for the respective action, and the returned result from the Controller is forwarded by the view back to the browser.

    Anyways, that was about MVC. So in RoR we always have to create these files for different functions.
    For our Article System, our objective is to create a system in RoR

    1. Create Database ‘articlesystem_development’
    2. Take Article input from User
    3. Display All the Article
    4. Edit Article
    5. Delete Article

    If we were to use PHP, this system would take not less than 50 Lines of Code, but in RoR …. you`ll soon find out;)

    1. Firstly open up Command Prompt (Start->Run) type cmd and click OK, then lets navigate to our ruby folder and create our application.
      The Syntax to create a new application is

      rails -d mysql appname

      So type

      C:\ruby\bin\>rails -d mysql articlesystem

      A long list of files & directories are created.

    2. Before editing, Create a database articlesystem_development through phpmyAdmin. If you don`t know how to create a database then refer to Tutorial : Creating a Content Management System using PHP/MySQL else you can use the mysql command to create the database.
      1. Goto Start->Run. Type cmd and hit Enter. You will be prompted with the command prompt.
      2. type mysql -u username -p, hit enter
      3. It`ll prompt you for your password. Enter the password.
      4. Once your inside the console type CREATE DATABASE databasename \g i.e mysql>CREATE DATABASE databasename \g
      5. If you see the following then you have successfully created a database.
        mysql> create database boo \g
        Query OK, 1 row affected (0.00 sec)
        
    3. Good ! Now navigate to F:\server\ruby\bin\articlesystem\config and open the file ‘database.yml’ in wordpad.
      development:
      adapter: mysql
      encoding: utf8
      database: databasename
      username: username
      password: password
      host: localhost
      
      test:
      adapter: mysql
      encoding: utf8
      database: databasename
      username: username
      password: password
      host: localhost
      
      production:
      adapter: mysql
      encoding: utf8
      database: databasename
      username: username
      password: password
      host: localhost
      

      You will see the following,except the password area has not been edited yet so type in your mysql user password. (If you have followed the previous tutorial, then password is root), and please enter the same database name in all development,test and production.

    4. Save the file.
    5. Now comes the fun part. Open up Command Prompt. Now we need to create the tables for our database.
      Our database schema is

      id(int) title(string) article(text)
      1 test article bla bla bla.. lot of content

      The above post was just an example, so basically we have 3 attributes i.e id, title and article.

    6. We will be using rails “Scaffolding” feature to create these tables and link it with our system. Scaffolding as the name says, to clean the windows of a high rise building, scaffolding is used as support temporarily, similarly here we are going to use ’scaffold’ to generate links & code.
    7. Now type in
      c:\ruby\bin\articlesystem>ruby script/generate scaffold article title:string article:text
      

      You`ll see notice that rails creates another bunch of files

      C:\ruby\bin\articlesystem>ruby script/generate scaffold article title:string article:text
      exists  app/models/
      exists  app/controllers/
      exists  app/helpers/
      create  app/views/articles
      exists  app/views/layouts/
      exists  test/functional/
      exists  test/unit/
      exists  public/stylesheets/
      create  app/views/articles/index.html.erb
      create  app/views/articles/show.html.erb
      create  app/views/articles/new.html.erb
      create  app/views/articles/edit.html.erb
      create  app/views/layouts/articles.html.erb
      create  public/stylesheets/scaffold.css
      create  app/controllers/articles_controller.rb
      create  test/functional/articles_controller_test.rb
      create  app/helpers/articles_helper.rb
      route  map.resources :articles
      dependency  model
      exists    app/models/
      exists    test/unit/
      exists    test/fixtures/
      create    app/models/article.rb
      create    test/unit/article_test.rb
      create    test/fixtures/articles.yml
      create    db/migrate
      create    db/migrate/20080709183936_create_articles.rb
      C:\ruby\bin\articlesystem>
      
    8. I know, there are hell lot of files being created !! :P Anyways, I`ll just tell you what RoR has generated, it has managed to create
      • article.rb -> Model inside c:\ruby\bin\articlesystem\app\models\,
      • articles_controller.rb -> Controller (Which has methods of all the functions i.e Create,delete,showall,edit) inside c:\ruby\bin\articlesystem\app\controllers
      • index.html.erb, edit.html.erb, new.html.erb, show.html.erb -> View Files (More like templates which determine the looks of the site) inside C:\ruby\bin\articlesystem\app\views\articles

      Remember MVC … you can check the code out. But don`t modify any right now.

    9. Now all you have to do is create the tables of the database. Hold on your horses, no need of logging in to mysql to create your tables. Just type this code
      C:\ruby\bin\articlesystem>rake db:migrate
    10. rake db:migrate -> executes the database schema RoR had generated automatically,
      Not only did RoR generate so many files, It also generated the table schema for articles.

      # This file is auto-generated from the current state of the database. Instead of editing this file,
      # please use the migrations feature of Active Record to incrementally modify your database, and
      # then regenerate this schema definition.
      #
      # Note that this schema.rb definition is the authoritative source for your database schema. If you need
      # to create the application database on another system, you should be using db:schema:load, not running
      # all the migrations from scratch. The latter is a flawed and unsustainable approach (the more migrations
      # you'll amass, the slower it'll run and the greater likelihood for issues).
      #
      # It's strongly recommended to check this file into your version control system.
      
      ActiveRecord::Schema.define(:version => 20080709183936) do
      
      create_table "articles", :force => true do |t|
      t.string   "title"
      t.text     "article"
      t.datetime "created_at"
      t.datetime "updated_at"
      end
      
      end
      
    11. Voila !!! RoR has just created all the necessary tables for you in mysql. Don`t believe me ? go check your database. Don`t be amazed, thats nothing, now time to test our code, invoke the ruby web server
      C:\ruby\bin\articlesystem>ruby script/server
    12. Point your web browser to http://localhost:3000/articles
    1. Congratulations !!! you have developed your first application in Ruby on Rails 2. Play around with the application, see what all you can do. RoR by default creates the delete,edit,create,show functions for your application when using “scaffold”. You can add articles, delete em, edit em or view all of em, from just 3 lines of code ;)

    This is the power of RoR !! Although I wrote a very basic tutorial (since I had less time), this tutorial covers a little theory of RoR’s architecture, how to create an application using rails in development mode, how to create a scaffold and finally to rebuild the database using rake db:migrate.

    So I hope you had fun, you can modify the view files in C:\ruby\bin\articlesystem\app\views\articles if you want to change the look of it.

    You can make the application start directly from http://localhost:3000/ I`ll be showing that, in my next tutorial for RoR which includes basic link routing, some more extra features to our article system. In the next tutorial I`ll also discuss about the singular & plural concepts in RoR which are very important. Till then Chow !

    discuss this topic to forum

    relation tutorial

    No relevant information

    Category

      Database Related (2)
      Getting Started (11)
      Helpers (5)
      Image Manipulation (2)
      Security (4)

    New

    Hot