Ruby on Rails

From Triangle Wiki

Jump to: navigation, search

Contents

Introduction

Ruby is a simple and powerful object-oriented programming language, created by Yukihiro Matsumoto (who goes by the handle "matz" in this document and on the mailing lists).

Like Perl, Ruby is good at text processing. Like Smalltalk, everything in Ruby is an object, and Ruby has blocks, iterators, meta-classes and other good stuff.

You can use Ruby to write servers, to experiment with prototypes, and for everyday programming tasks. As a fully integrated object-oriented language, Ruby scales well.

Ruby features:

  • Simple syntax,
  • Basic OO features (classes, methods, objects, and so on),
  • Special OO features (Mix-ins, singleton methods, renaming, …),
  • Operator overloading,
  • Exception handling,
  • Iterators and closures,
  • Garbage collection,
  • Dynamic loading (depending on the architecture),
  • High transportability (runs on various Unices, Windows, DOS, OSX, OS/2, Amiga, and so on)

Ruby on Rails

Rails is an open source Ruby framework for developing database-backed web applications. What's special about that? There are dozens of frameworks out there and most of them have been around much longer than Rails. Why should you care about yet another framework?

What would you think if I told you that you could develop a web application at least ten times faster with Rails than you could with a typical Java framework? You can--[i]without[/i] making any sacrifices in the quality of your application! How is this possible?

Part of the answer is in the Ruby programming language. Many things that are very simple to do in Ruby are not even possible in most other languages. Rails takes full advantage of this. The rest of the answer is in two of Rail's guiding principles: less software and convention over configuration.

Less software means you write fewer lines of code to implement your application. Keeping your code small means faster development and fewer bugs, which makes your code easier to understand, maintain, and enhance. Very shortly, you will see how Rails cuts your code burden.

Convention over configuration means an end to verbose XML configuration files--there aren't any in Rails! Instead of configuration files, a Rails application uses a few simple programming conventions that allow it to figure out everything through reflection and discovery. Your application code and your running database already contain everything that Rails needs to know!

Installation on Windows machine

Required softwares:

First of all, download and install the latest stable version of ruby, typically in c:\ruby. Use a windows one-click installer, this will automatically set up the PATH and other important variables.

Next, you need to install a number of gems through ruby. Gems are an online repository of ruby extensions, very much like PEAR for PHP or CPAN for Perl. Run the following commands from anywhere in your command prompt:

gem install -y ajax_scaffold_generator
gem install -y daemons
gem install -y mysql
gem install -y rails 
gem install -y mongrel
gem install -y mongrel_service
gem install -y rake
gem install -y rapt
gem install -y redcloth

Then download RMagick-win32 and unzip it in c:\ruby, keeping directory structure. Move into the folder and perform the following commands:

C:\ruby>cd [rmagick_folder]
C:\ruby\[rmagick_folder]>gem install Rmagick-1.9.2-IM-6.2.4-6-win32.gem
C:\ruby\[rmagick_folder]>ruby postinstall.rb

Now create your rails application:

C:\ruby>rails generate C:\wwwroot\rails

Edit the C:\wwwroot\rails\config\databases.yml file with your database settings

You now need to install the mongrel (ruby server) service:

C:\ruby>mongrel_rails service::install -N mongrel_rails -p 4000 -r "C:\wwwroot\rails\public"

This needs to be started by hand from the windows services screen, but can be set to start automatically each time you start windows.

What is important to realise here is that each mongrel server only services ONE rails application. If you change the name of your rails application, you will need to create a new service that reflects that change. Also, if you change your settings in databases.yml, you will need to restart the mongrel server before your changes are taking into account.

Finally, Apache needs some configuration directives in order to serve the rails application. Two modules need to be switched on:

LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_http_module modules/mod_proxy_http.so

Additionally, the following directives need to be added:

ProxyRequests Off
<Proxy *>
   Order deny,allow
   Allow from all
</Proxy>
<Directory "C:/wwwroot/rails/public">
   Options Indexes FollowSymLinks
   AllowOverride none
   Order allow,deny
   Allow from all
</Directory>
ProxyPass /rails/images !
ProxyPass /rails/stylesheets !
ProxyPass /rails/javascripts !
ProxyPass /rails/ http://127.0.0.1:4000/
ProxyPass /rails http://127.0.0.1:4000/
ProxyPassReverse /rails/ http://127.0.0.1:4000/

Then restart the server.

Your database tables MUST have their Primary Key called 'id' before any of this will work. There are ways to circumvent this, but they are complex, and I haven't yet explored them. Rails relies on conventions rather than configuration, hence this limitation.

Once you have a table ready (example here is domains, all tables MUST be plural), you can generate the ajax scaffold:

C:\wwwroot\rails>rails generate ajax_scaffold domain

A series of files is created, and your application is ready to use. Access http://127.0.0.1:4000/domains to see the amazing results!

External Links

Personal tools