Last time we talked about the “Rails pipeline”, we compared it to the ASP.NET pipeline to try to give a quick overview of what a new Rails programmer needs to know about how browser urls result in HTML pages.
We mainly talked about how Rails’ main job in life is to translate a given url into a method on an object. We continue where we left off – how does Rails know which HTML page to render after calling your method?
First, let’s back up a bit. Let’s add some more Rails jargon this time so you can start to get used to the terminology. Rails wants to take a url like http://www.mysite.com/racing/stats and figure out which class and which method it should call.
There’s a file called routes.rb which defines this routing, but by default the first thing after your domain name will be the controller class to instantiate. The second thing is the instance method to call.
So in our example Rails would look for a controller class called Racing in a file called /controllers/racing_controller.rb. It would try to instantiate an object of that class, then call a method you’ve written called stats. This method is called the action.
We’re almost home. Typically your controller method – I mean, action – will do some database lookups and assign values to some of the controller’s instance variables. But it won’t usually try to render any HTML (though it can). By default, if you don’t try to render any HTML (that is, don’t call the built-in render method), then Rails will assume it should send back an rhtml file you’ve written. But in order for rails to find it, the rhtml file you want rendered after your action method is called needs to follow a specific naming convention, which is /views/controller/action.rhtml.
So in our case, your rhtml file would be /views/racing/stats.rhtml. Stats.rhtml can implicitly access any public instance variables in the controller, and it probably will access variables you’ve used in your action method.
Summarizing:
Well, that’s it in a nutshell. We’ve avoided talking about the myriad ways you can customize this pipeline, but hopefully this gets you off the ground.