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
- Create Database ‘articlesystem_development’
- Take Article input from User
- Display All the Article
- Edit Article
- 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;)
- 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 israils -d mysql appname
So type
C:\ruby\bin\>rails -d mysql articlesystem
A long list of files & directories are created.
- 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.
- Goto Start->Run. Type cmd and hit Enter. You will be prompted with the command prompt.
- type mysql -u username -p, hit enter
- It`ll prompt you for your password. Enter the password.
- Once your inside the console type CREATE DATABASE databasename \g i.e mysql>CREATE DATABASE databasename \g
- 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)
- 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.
- Save the file.
- Now comes the fun part. Open up Command Prompt. Now we need to create the tables for our database.
Our database schema isid(int) title(string) article(text)
1 test article bla bla bla.. lot of contentThe above post was just an example, so basically we have 3 attributes i.e id, title and article.
- 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.
- 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>
- I know, there are hell lot of files being created !!
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.
- 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
- 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
- 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
- Point your web browser to http://localhost:3000/articles
- 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
