Ruby-on-Rails
  Home arrow Ruby-on-Rails arrow Page 3 - Getting Started with Ruby on Rails
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
RUBY-ON-RAILS

Getting Started with Ruby on Rails
By: A.P.Rajshekhar
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 17
    2006-08-01

    Table of Contents:
  • Getting Started with Ruby on Rails
  • Understanding the Terminology of Ruby-on-Rails
  • A Ruby-on-Rails Application, Step by Step
  • Rails in the Real World

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Getting Started with Ruby on Rails - A Ruby-on-Rails Application, Step by Step


    (Page 3 of 4 )

    Since any RoR application has to follow the MVC-Model II, the steps we must take involve the implementation of the three components of MVC. Yet the way the implementation goes, the components need not to be implemented from scratch as RoR has two points as guiding philosophies: DRY, or Don’t Repeat Yourself; and CoC, or Convention over Configuration.

    The first point means that most of the code that gets repeated has been abstracted out into the framework. The second point means that all the management of the application is done by the framework on the basis of Conventions. The following are the steps essential for any kind of application to be developed:

    1. Creation of Application structure.

    2. Creation of the Controller.

    3. Implementing the View.

    4. Configuring the database.

    5. Starting the server.

    Of these, the fourth step comes into the picture only when the application requires database access. Each of these steps consists of one or more commands to create the corresponding components. So here is what’s to be done at each step:

    Creation of the Application Structure

    The first step in implementing any application is defining the directory structure in such a way that it reflects the components and the helping classes’ position within the application. For other frameworks, this has to be done by hand and linked with the framework by using the configuration files. In RoR, everything is done with a single command -- rails -- which takes the name of the application to be created as a parameter. There are configuration switches for the rails command but that will be discussed in the future. Coming back to creating the application structure, if the an application named demo has to be created, then change the directory where the structure has to be created and give the following command at the command line:

    rails demo

    and RoR will respond by printing out the directory structure created like this:

    If a dir (or ls based on the OS) is give inside the newly created directory, once the command is completed, one would see something like this:

    The two directories of importance at present are “app” and “config.” The “app” directory contains the subdirectories having the files implementing the Controller and View logic of the application. At present these subdirectories contain nothing of importance. Hence the application is just skeletal. To make this skeletal structure work, a Controller is required. How to create a Controller is covered in the next step.

    Creation of the Controller

    Since the Controller orchestrates the application, without the Controller, it would be nearly impossible to make the application work. That’s true with RoR. So to create the Controller, RoR provides a command called generate. In reality, the generate command is a Ruby script in the “script” directory. This command has many more functions, which becomes the center point of the next step. For now, to create a controller named ‘Say’ in the “demo” (or whatever may be the application name) directory, the following command has to be issued:

    ruby script/generate controller Say

    The generate command takes the type of the MVC component as the first argument and the name of the component as the second argument. Here the type (constituent) of the MVC component is Controller and the name of the Controller is Say. On giving the command RoR responds with:

    The significant aspect of the output is the three files and a directory that are generated: the directory “say” under app/view directory, the say_controller.rb under app/controllers directory and say_helper.rb under the app/helpers directory. Let's leave the “say” directory for now. The Controller say_controller.rb is the Controller of this application. Here also CoC comes into the picture. The Controller is suffixed with “_controller”. The next file is the say_helper.rb. It provides the services required by the Controller. If one looks at the say_controller.rb, the code would be something like this:

    class SayController < ApplicationController

    end

    The above code means that the SayController is inheriting from the ApplicationController class. Each control action would have to be defined as a method of the SayController class. So to create a control action (or controller) called hello, one would have to define a method called hello as a member method of SayController class thus:

        class SayController < ApplicationController

    def hello

    end

    end

    Now the Controller for the application demo is created. Though it does nothing much, it’s a beginning. The next step is creating the View component because without a corresponding View, the Controller’s orchestration proves nothing.

    Implementing the View

    To implement the View, RoR provides a templating technique in the form of RHTML, using which, the Ruby statements can be embedded into the HTML pages. To create the View, first an RHTML file is created. The name of the file has to be the same as that of the Controller. For example, to create a View for the Controller (or control action) hello, an RHTML file with the same name has to be created inside the app/view folder of the application. So the required RHTML file would be hello.rhtml. To make it more clear following could be the content of the file:

    <html>

    <head>

    <title>Hello, Rails!</title>

    </head>

    <body>

    <h1>Hello from Rails! The time now is<%=Time.now%> </h1>

    </body>

    </html>

    That completes the View part of the RoR.

    Configuring the database

    To configure a database connection for the application is as easy as changing two to three parameters in a file. But before that following things have to be done:

    First, the database (or tablespace in Oracle's case) has to be suffixed with  _development for development purposes, _test for testing purposes and _production for release purposes. For example, if the database name (for MySQL) is scm, then scm_development should be the name of database used for development, scm_test for debugging and scm_production for the release version of the database. 

    Then, the pure Ruby-based database driver is required for connectivity, otherwise connection time errors will be thrown during the database configuration step.

    With these points in mind, let's look at database configuration using the database.yml file. When the application is created, RoR creates a database.yml file. The required database configuration has to be done here. Typically any database.yml will look like this:

        development:

          adapter: mysql

          database: book_development

          username: root

          password:

          host: localhost

    The above configuration is for development purposes. That's all for database configuration. 

    Starting the Server

    The server for RoR comes built into each application. The server resides inside the “scripts” folder. To start the server the command is as follows:

    ruby script/server

    and the rails would give the something like the following output:

    => Rails application started on http://0.0.0.0:3000

    [2005-02-26 09:16:43] INFO WEBrick 1.3.1

    [2005-02-26 09:16:43] INFO ruby 1.8.2 (2004-08-24) [powerpc-darwin7.5.0]

    [2005-02-26 09:16:43] INFO WEBrick::HTTPServer-start: pid=2836 port=3000

    Ignore the dates. Just look at the port. It is 3000., and 0.0.0.0 is taken as the server address. It is the equivalent of localhost. Now to launch the application the following has to be typed at the address bar of any browser:

    http://localhost:3000/demo/say/hello

    The interesting aspect is how the URL is formed. It says nothing about RHTML or the controller file. Instead it is using the name that we passed as a parameter to the generate script and the function created inside the controller class. In essence, the URL is of the form:

    http://<host>:<port>/<app_name>/<controller_name>/<name_of_the_controller
    _method>

    Pictorially it is:

    That completes the steps involved in creating a RoR application. In the next section, I will be implementing a CRUD form.

    More Ruby-on-Rails Articles
    More By A.P.Rajshekhar


       · RoR is the latest in the field of server-side frameworks. When compared to other...
     

    RUBY-ON-RAILS ARTICLES

    - Iterating and Incrementing Strings in Ruby
    - Comparing and Manipulating Strings in Ruby
    - Strings in Ruby
    - Ruby On Rails: Making Your First Dynamic Site
    - Ruby on Rails: Beginning Rails
    - Ruby: Modules, Mixins, Fixins, and Rails
    - Controlling Information Access with the Rail...
    - URLs, Filters and the Rails Action Controller
    - Flash and the Rails Action Controller
    - Rails Action Controller
    - Dropping and Sorting with AJAX and script.ac...
    - Drag and Drop with script.aculo.us and Rails
    - Introducing script.aculo.us
    - Ruby Classes and Objects
    - Ruby Loops







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek