. 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

1 Comments:

Anonymous Anonymous said...

see this onjava article -
http://www.onjava.com/pub/a/onjava/2005/11/16/ruby-the-rival.html
-Manoj

9:02 PM  

Post a Comment

<< Home

Valid XHTML 1.0!