Page 154 - DCAP312_WEB_TECHNOLOGIES_II
P. 154
Web Technologies-II
Notes While this model works perfectly, there is a downside. For some object types, the generic
representation is not quite sufficient. We might have to override or augment the default behavior.
In a truly object oriented environment we would simply subclass my ObjectController. The sub-
classed controller could neatly alter the behaviour of the ancestor class. The problem is, we have
no idea how we can direct RoR to instantiate the sub-classed controller. Is this even possible?
The following controller hierarchy:
ActionController::Base
ApplicationController
VehicleController
Vehicle::CarController
Vehicle::BikeController
While this goes some ways to address my design issue, one of the things it does not seem to do
“out-of-the-box” is to allow me to override views.
Consider my VehicleController implements the method show. In app/views/vehicle we have
a file show.rhtml. We never instantiate a VehicleController object though: We either instantiate
a Vehicle::CarController or a Vehicle::BikeController. Either of these two controllers inherits the
show method from VehicleController.
However, there is now an issue with selecting the views. Consider send the show message to a
Vehicle::CarController. Ruby correctly invokes the VehicleController.show method which might
call render(:action => “show”). The preferred behaviour is now that if a show.rhtml was found
in app/views/vehicles/car, rails should render that template. If none was found there, it should
try and pick the show.rhtml from app/views/vehicle and if that did not exist ether, it should fail.
ParentPage.cs
public class ParentPage : System.Web.UI.Page
{
protected String _UserName;
public ParentPage()
{
this.Load += new System.EventHandler(this.Page_Load);
}
protected void Page_Load(object sender, EventArgs e)
{
UserName = Session[ “UserName”].ToString();
}
}
page1.aspx.cs
public partial class Page1 : ParentPage
{
protected void Page_Load(object sender, EventArgs e)
{
}
}
page2.aspx.cs
public partial class Page1:ParentPage
{
protected void Page_Load(object sender, EventArgs e)
148 LOVELY PROFESSIONAL UNIVERSITY