Ruby on Rails: Beginning Rails - Controlling the Situation
(Page 2 of 4 )
To make an application work, you first need a controller. The controller is the evil overseer, the big boss, the big cheese, the head honcho, the imperial slave-driver. He is that guy that says "I think the benefits of slavery outweigh its drawbacks" and then makes you carry heavy boulders back and forth.
To create this big galoot we use the following ruby command: ruby script/generate controller controllername. Below we will create a controller named Willis, in the hopes of later learning what the heck he was talking about:
ruby script/generate controller Willis
If you go to rubymyfirsttimeappcontrollers you will see a file name willis_controller.rb. This is the file you just created. You can open it to view the code if you like; just be sure not to change it.
A Call to Action
While having a controller is all fine and dandy, it's pretty worthless if it isn't issuing orders for its minions to follow. This is where Actions come in. Actions are what make your application do things. The following action will make the browser display some text. Here is how you add your action to the controller:
Open the myfirsttime.rb controller file. It will show the text:
class WillisController < ApplicationController
end
We are going to insert some text in-betwixt the first line and the end line:
def whatyoutalkingbout
end
This creates a method called whatyoutalkingabout. Typically you would use something simpler, but I wanted to match the theme. And it's my article, so there. So now the file should read:
class WillisController < ApplicationController
def whatyoutalkingbout
end
end
And lastly, in order to complete our beautiful web page, we must create a View.
Next: A View...To a Kill (Duhn Duhn Dun!!!) >>
More Ruby-on-Rails Articles
More By James Payne