Login
Username:

Password:


Lost Password?
Register now!
Page « 1 2 3 4 (5) 6 7 8 9 »
Articles : Web Development Using the Ruby on Rails
on 2008/7/27 23:54:31 (1981 reads)
Articles


The Controller

The classes that make up the Controller layer are bundled together in the ActionController module. The Controller layer in a Web application is like a policeman regulating traffic, but instead of regulating car traffic, it controls incoming and outgoing Web/database traffic. In Rails, you typically have a whole bunch of these policemen (or ActionController classes). Each one has its own area to control and it is entirely up to the developer to specify these areas. You could have one that is in charge of the contracts and another one that is in charge of your employees:

class ContractsController < ApplicationController
end

class EmployeesController < ApplicationController
end

As its name implies, a Controller class controls something, so it has to be able to 'do something' with the contracts or employees. The Controller classes in the previous example don't seem very capable of doing anything like storing a new contract, or firing an employee. They are powerless policemen. In Rails terms, they still need 'actions'. Actions define what a Controller is able to control, like a policeman can 'give a ticket', or 'stop a car'. Without actions there would not be much happening in your application. You specify actions for a Controller simply by defining methods in a Controller class:

class EmployeesController < ApplicationController

def show
@employee = Employee.find(params[:id])
end

end


This is not much code, but a lot of stuff is done for you behind the scenes, a testimony of the ease with which you write Rails Web applications. Most other popular Web application frameworks would require a lot more code and configuration to achieve the same result. Let us lift the curtains and peek at what is happening in the EmployeeController. The play begins with a user clicking on a link 'http://www.myrailsapp.com/employees/show/1'.

Now what happens first is that Rails starts dissecting the url to see which action Controller needs to perform. We notice here another convention: the url needs to comply with a specific format. In this case the format is 'http://www.myrailsapp.com/:controller/:action/:id'. Rails will simply look at the strings separated by a forward slash and will confer meaning to them based upon their order. The first part is 'employees', which maps to the Controller name. The second part is 'show', which maps to the name of the action the Controller needs to execute. The third and final part is the id of a record in our database. This very simple url convention lets you write clean and logically constructed urls. As a developer it is really easy to figure out what is going to happen when looking at the url without having to trace through various mapping files as is the case in many other Web frameworks. Just by looking at the url Rails knows to go to the EmployeesController and calls the public method 'show', without writing any code yet! Once inside the method 'show' we see one line of code:

@employee = Employee.find(params[:id])

Page « 1 2 3 4 (5) 6 7 8 9 »
Printer Friendly Page Send this Story to a Friend Create a PDF from the article
Share The Knowledge

Jobs