. View this ad or post your own Ruby Job!

Sunday, November 13, 2005

Building an application with Ruby on Rails - Controllers

What have I found out so far?
  • A simple understanding of Ruby programming language
  • Getting started with Ruby on Rails
In my previous post, we created the jauth application's directory structure. Most of our development work will be creating and editing files in the C:\InstantRails\rails_apps\jauth\app subdirectories.
  • The controllers subdirectory is where Rails looks to find controller classes. A controller handles a web request from the user.
  • The views subdirectory holds the display templates to fill in with data from our application, convert to HTML, and return to the user's browser.
  • The models subdirectory holds the classes that model and wrap the data stored in our application's database.
  • The helpers subdirectory holds any helper classes used to assist the model, view, and controller classes. This helps to keep the the model, view, and controller code small, focused, and uncluttered.
Controllers - In my previous post, I had mentioned that you need to keep the existing command window open with the web server (WEBrick) running.
  • In the command window, go to folder C:\InstantRails\rails_apps\jauth
  • To create a new controller class type in this command window - ruby script\generate controller MyTest. This creates a file named my_test_controller.rb containing a skeleton definition for the class MyTestController.
  • Now type the url http://127.0.0.1:3000/my_test/ this displays Unknown action No action responded to index. The my_test part of the URL maps to the newly created controller. Now it seems that Rails tried to find an action named index in this controller but couldn't. Let's fix that.
  • Add an index method (shown in the code below) to your controller class and then refresh your browser. However, we see an error Template is missing Missing template ./script/../config/../app/views/my_test/index.rhtml
  • We shall remove this error temporarily, by creating a file index.rhtml (as shown below) in the folder C:\InstantRails\rails_apps\jauth\app\views\my_test
#my_test_controller.rb
class MyTestController < ApplicationController
def index
disp = 'Hello World'
end
end
and the file index.rhtml
<html>
<head><title>jauth Application</title></head>
</html>
The MyTest controller class will not be used again in our application. In my next post, we shall look at Models, though we would re-visit Controllers again later.


First Post | Previous | Next


Technorati Tags:
Blogs linking to this article

Friday, November 11, 2005

Ruby on Rails

What have I found out so far?
  • Definition of Ruby
  • Ruby Windows Installer
  • Interactive Ruby - Open a new DOS window and type irb --simple-prompt A prompt appears as >>
  • Run a Ruby program by typing ruby hello.rb
  • Learnt some features of Ruby like comments, naming rules, variables, constants, strings etc.
  • Various Text Editors/IDEs like SciTE, NotePad2
  • Constructs like the if else and while
  • Some methods in Ruby.
  • Arrays
  • Writing one's own methods
  • Writing one's own class
Armed with the elementary knowledge of Ruby gained so far, I am now going to explore Ruby on Rails. Rails is a full-stack, open-source web framework (support structure) in Ruby, that closely follows the Model View Controller (MVC) architecture for writing real-world applications with less code and no cumbersome XML configuration files. Simply put, Rails is an open source Ruby framework for developing database-backed web applications.

To start using Ruby on Rails, I decided to use Instant Rails a one-stop Rails runtime solution containing Ruby, Rails, Apache, and MySQL, all preconfigured and ready to run. No installer, you simply drop it into the directory of your choice and run it. It does not modify your system environment. Instant Rails is completely self-contained in its own directory tree and should not interfere with any existing Ruby or Rails installation. Anything you run within IR will use its private copy of Ruby and Rail. I now downloaded and installed Instant Rails on my Windows XP Pro system. It has created a folder C:\InstantRails on my system. You need to follow the instructions for setting up Instant Rails properly.

I am now going to create a simple Ruby on Rails application called jauth. The steps are:
  • Go to folder C:\InstantRails and double-click on file InstantRails.exe
  • On your System Tray you will see an I. Right click on this, and then click on Rails Applications/Manage Rails Applications...
  • A window pops-up. Now click on Create New Rails App (open console window) A DOS window open and you are in the folder C:\InstantRails\rails_apps
  • Next, type rails jauth
  • Keep this DOS window open
Rails is both a runtime web app framework and a set of helper scripts that automate many of the things you do when developing a web application. With the last command, we have created a jauth subdirectory containing a complete directory tree of folders and files for an empty Rails application.

A Rails web application can run under virtually any web server, but the most convenient way to develop a Rails web application is to use the built-in WEBrick web server. Let's start this web server and then browse to our jauth application. To do this
  • Right click on I in your system tray
  • Now click on Rails Applications/Manage Rails Applications...
  • Select jauth and click on the button Start with WEBrick. This open a DOS window.
Now open your browser and browse to http://127.0.0.1:3000/. You should see a page that says - Congratulations, you've put Ruby on Rails!

In my next post, I shall start building my jauth application.


First Post | Previous | Next



Technorati Tags: ,
Blogs linking to this article

Writing One's Own Class in Ruby

What have I found out so far?
  • Definition of Ruby
  • Ruby Windows Installer
  • Interactive Ruby - Open a new DOS window and type irb --simple-prompt A prompt appears as >>
  • Run a Ruby program by typing ruby hello.rb
  • Learnt some features of Ruby like comments, naming rules, variables, constants, strings etc.
  • Various Text Editors/IDEs like SciTE, NotePad2
  • Constructs like the if else and while
  • Some methods in Ruby.
  • Arrays
  • Writing one's own methods


Today, I am going to have a quick look at Writing one's own Class in Ruby.

Let us look at a simple program.
class MotorCycle
# Every class must contain this method
def initialize(make, color)
# Instance variables
@make = make
@color = color
end
def startEngine
if (@engineState)
puts 'Engine Running'
else
@engineState = true
puts 'Engine Idle'
end
end
def dispAttr
puts 'Color of MotorCycle is ' + @color
puts 'Make of MotorCycle is ' + @make
end
m = MotorCycle.new('Yamaha', 'red')
m.startEngine
m.dispAttr
m.startEngine
end


First Post | Previous | Next



Technorati Tags:
Blogs linking to this article

Sunday, November 06, 2005

Writing One's Own Methods in Ruby

What have I found out so far?
  • Definition of Ruby
  • Ruby Windows Installer
  • Interactive Ruby - Open a new DOS window and type irb --simple-prompt A prompt appears as >>
  • Run a Ruby program by typing ruby hello.rb
  • Learnt some features of Ruby like comments, naming rules, variables, constants, strings etc.
  • Various Text Editors/IDEs like SciTE, NotePad2
  • Constructs like the if else and while
  • Some methods in Ruby.
  • Arrays

Today, I am going to look at writing one's own methods in Ruby.

Let us look at a simple program.
# A simple method
def hello
puts 'Hello'
end
#use the method
hello
# Method with an argument
def hello1(name)
puts 'Hello ' + name
return 'success'
end
result = hello1('satish')
# Check the return value
puts result


First Post | Previous | Next



Technorati Tags:
Blogs linking to this article

Valid XHTML 1.0!