Navigate

 

 

Article Links

 

 

Translate / i18n

 

 

Article Indexes

 

 

Advertisement

 

 

Article License

 

  • Creative Commons License
 

Rapid Web Development with Rails

Ruby on rails is a rapid web application development framework. learn how rails can help you keep your web projects on track and on budget with record delivery times.

 

by  John Buswell     

 

 

Ruby on Rails is an open Source web framework that is optimized for rapid web application development. Rails provides a structured framework that makes development feel natural and easy to maintain. Ruby is a relatively easy programming language to learn; any programmer with even just a vague idea of Java, Python or another object orientated programming language will pickup Ruby fairly quickly. With a little effort, Ruby on Rails will allow you to quickly develop and modify web applications in an efficient and cost-effective manner. Projects typically go over budget due to unrealistic time frames, unanticipated problems or the varying skills of developers on the team. Ruby on Rails will help mitigate these traditional problems by providing a fast and easy to follow framework for building web applications. Ruby on Rails applications are database centric, and Rails provides an object-relational management layer called ActiveRecord that significantly reduces the headaches caused trying to map object based programming languages with data contained in relational databases. Rails provides instant gratification - you make a change, you point your browser, and you see the change in effect. It's instant. This has a key benefit when demonstrating an application or proof of concept application to a client. A lot of problems around customer web applications center around the ability of the engineering team to communicate with the customer and many companies have a management “translator” who interacts between the customer and the engineers. Rails eliminates this need, as the customer can see the application in real time. If the developer misinterpreted the customer's feature request, the developer can quickly load up an editor, modify the feature and show the customer the change instantly. This allows for faster and easier communication with the customer and will increase the customer's atisfaction with your business. In fact, many non-technical customers who have dealt with developers using traditional means, such as the document, develop, demonstrate, document, develop, and demonstrate cycles, will be highly impressed by the instant nature of rails. I had one businesswomen refer to it as “magic”. If you are still not convinced about the speed and simplicity of Rails, then I suggest you take a look at the screencasts that are available over at http://www.rubyonrails.com/screencasts.

 

Getting started

Ruby is an interpreted, high-level object orientated programming language, and in some situations it may not perform as fast as lower-level languages such as C. However, there are a number of things you can do to improve performance. Slow running programs typically have a few locations where the processes are heavily hit. You can use the Benchmark module and code profiler that come with Ruby to help locate and fix these types of problems. Since most web developers write code these days in high level languages such as Python, Perl and Java anyway, and Ruby stacks up very well in performance compared to these languages, so the performance hit, if any for a specific application is well worth the cost and time saving benefits of the language. If you would like to learn more about the Ruby Language itself you can look at http://www.ruby-lang.org/. If you would prefer a book, I would highly recommend Programming Ruby: The Pragmatic Programmer's Guide by Dave Thomas.

 

RubyGems is the package management system for Ruby. The command was shortened to gem. Gems interacts over the Internet with rubyforge.com, a huge repository of software code maintained by the rapidly growing Ruby community. RubyForge provides access to over 1,100 hosted projects, so you will often find something you are looking for without having to code it yourself.

 

Building the environment

Rails can be integrated with Apache, Lighttpd and many other web servers that support SCGI or FastCGI. Lighttpd is a good option, and was reviewed in last month's issue of O3. To get started you will need Ruby and RubyGems. The recommend builds are listed on the Rails download page (http://www.rubyonrails.com/down).

 

Building ruby

The recommended release for Rails is currently 1.8.2. Untar it, and go through the usual POSIX motions (./configure && make && make install). If you want to make use of the ruby documentation tool (ri) then you will also want to run make install-doc.

 

Building rubygems

Installing RubyGems is trivial - simply untar and run ruby setup.rb from the rubygems-0.8.11 directory.

 

Installing rails

Now that you have both Ruby and RubyGems installed, you can use the gem command to grab rails. You will need to make sure that you have Internet access on the system you are using Ruby on before running the following command:

 

 

  • gem install rails –include-dependencies

 

 

As simple as that, rails is now on your system. Keep in mind that Rails is database centric, meaning you will need to have some kind of database available to you in order to utilize Rails. A wide range of databases are supported including MySQL and PostgreSQL. For this article, I used PostgreSQL 8.1.

 

Architecture

The Rails architecture is built around MVC, ActiveRecord (Object-Relational database management) and URL mapping. These are core concepts for using Rails so we shall look at them briefly.

 

MVC

MVC is an architecture designed by Trygve Reenskaug back in 1979. It is a Model, View, Controller architecture. The model manages the state of the application. It is not just a representation of data since it enforces the business rules that are applicable to that data. Such rules might be that shipping within the state should always be UPS Ground because it will get there overnight regardless, that sales tax is charged based on the destination within the state or that customers outside the European Union are not charged VAT. The view is the visual presentation of the data to the user. This is the user interface, and from a rails point of view, is usually the HTML that is displayed to the user's browser. The view might be different for different perspectives, so the administrator might see all the users, while a single normal user may only see their preferences. As the name suggests, a controller maintains control of the application. It takes events from the user, interacts with the model (data) and provides a new view to the user.

 

Activerecord

Anyone familiar with injecting SQL commands into their PHP or C code is engaged in whats called Database-centric programming. If you know SQL, then this is a relatively easy and pain free method of obtaining data from a database quickly and easily. If you are a good programmer, you probably write centralized functions that are called throughout your code, so that if you need to modify how your application interacts with the database or to change databases, you have a relatively pain free method of doing so. Unfortunately, not everyone is a good programmer, and maintaining code over time with hundreds of SQL statements throughout thousands of lines of code can become a very painful task. Rails utilizes a method called Object/Relational Mapping where database tables are mapped to object classes. This is not an easy thing to achieve and often requires considerable amounts of XML configuration files. ActiveRecord solves this problem by providing an ORM layer within Rails. ActiveRecord, like many aspects of Rails, relies on convention and sensible defaults, making it easy to modify and customize to your specific needs. ActiveRecord integrates seamlessly with the rest of the Rails framework.

 

URL mapping

The last important concept to grasp is that Rails utilizes URLs to map requests to a specific controller and action.

 

 

  • http://192.168.99.202/rails/helloworld/greet/hello

 

 

In the URL above the http://192.168.99.202/rails/helloworld/ identifies the application, the greet/ selects the controller (greet) and the hello provides the action to invoke. However, Rails is reasonably flexible about how it parses the URLs, so it is possible to parse them differently should you wish to do so.

 

Building a simple application

We will now walk you through the creation of a simple “hello world” application in rails. The steps we will take will be to create the application, add a controller, add a view, add some dynamic content and then test the application.

 

Creating the application

Creating applications with rails is trivial. With rails installed earlier, we simply change directory over to where we want to store our new project then run rails , for our application we will run rails helloworld. This will create a new directory called helloworld, and populate it with the default files for rails. To start the test server (assuming your not running it via FastCGI under another web server) simply run helloworld/scripts/server and then go open up a new terminal.

 

Adding a controller

Next we will need to add a controller. We will add one called Greet as it will manage the greeting to the user.

 

 

  • ruby script/generate controller Greet
  • exists app/controllers/
  • exists app/helpers/
  • create app/views/greet
  • exists test/functional/
  • create app/controllers/greet_controller.rb
  • create test/functional/greet_controller_test.rb
  • create app/helpers/greet_helper.rb

 

 

Now we edit apps/controllers/greet_controller.rb. As you can see, it already has some code in there for us:

 

 

  • class GreetController < ApplicationController
  • end

 

 

We will add the action “hello” to our controller:

 

 

  • class GreetController < ApplicationController
  • def hello
  • end
  • end

 

 

If you point the browser at the server http://192.168.99.202:3000/greet/hello/, you will get a message about a missing template. This is produced because we haven't added the view yet. The view is added under app/views/greet/.rhtml. In our case hello.rhtml.

 

Creating the view

Adding the view is simple. We just create hello.rhtml with some html in it:

 

  • <html>
  • <head>
  • <title>Hello World!</title>
  • </head>
  • <body>
  • <b>Hello World!</b>
  • </body>
  • </html>

 

 

Point the browser at Rails again, and now we get Hello World!

 

Adding some dynamic content

Just to demonstrate how easy it is to add dynamic content in Rails we will modify our code now to display the time. To do this, we need to update the controller (greet_controller.rb) to capture the time by modifying the hello action to:

 

 

  • def hello
  • @time = Time.now
  • end

 

 

Then you simply edit the hello.rhtml file to display the time:

 

 

  • <body>
  • <b>Hello World!</b><p />
  • The time is now <%= @time %>.
  • </body>

 

 

It's as simple as that. We now get the following in our browser window:

 

 

  • Hello World!
  • The time is now Mon Dec 19 12:57:43 EST 2005.

 

 

Conclusion

Overall, Ruby is an intuitive and easy language to learn and Ruby on Rails provides a fast and optimized framework for developing web applications quickly. Not only is it great for prototyping and proof of concept applications, but you can simply expand upon the original proof of concept code and utilize it as a base for the actual application. Ruby on Rails is well worth a look whether you're looking for a simple in-house project or trying to keep unrealistic project deadlines with traditional web solutions. The bottom line for businesses, especially web development businesses, is that Ruby can save you time and resources regardless of the size of the project.

 

 

  •  
  • About o3:
  •  
  • o3 is a FREE online publication. o3 is published using open source software. The focus of o3 is on the use of Free and Open Source (FOSS) software in Enterprise and Business environments.
  •  
  •  
Sponsored by
Copyright © 2009 PRO OnCall Technologies LLC