Ruby On Rails: Making Your First Dynamic Site - Viewing Your Page
(Page 2 of 4 )
Now let's view the page. First, start your WEBrick server by typing in:
>ruby script/server
Remember that to view the page we have to type in http://localhost:3000/ followed by the name of the controller and the view, or in this instance: http://localhost:3000/weight/enormous
If you followed my instructions you should see something that looks like this:
Some math to calculate your enormity
We will use two numbers: The first represents your physical weight, while the second represents your implied weight, which is determined by your effect on gravity
Your weight in pounds: 36000000000
Creating Loops
In this example we will mix some HTML and Ruby together to create a loop to insult the 1980s wrestler, the Iron Sheik. Open up your enormous.rhtml file and let's change it to the following:
<html>
<body><h1 align="center">Hulk Hogan versus The Iron Sheik!</h1>
<p>The Iron Sheik looks angry as the crowd chants:</p>
<% 10.times do %>
USA!
<% end %>
<h2 align="center"> HULKAMANIA IS RUNNIN' WILD BROTHA!</h2>
</body>
</html>
Run the code and see what happens. The word USA! should have printed ten times.
Now let's spruce it up some to show how the audience starts off crazy, simmers down some, then does it all over again:
<html>
<body><h1 align="center">Hulk Hogan versus The Iron Sheik!</h1>
<p>The Iron Sheik looks angry as the crowd chants:</p>
<% 2.times do %>
<h1>USA!</h1>
<br>
<h2>USA!</h2>
<br>
<h3>USA!</h3>
<br>
<h4>USA!</h4>
<br>
<h5>USA!</h5>
<br>
<h6>USA!</h6>
<br>
<h5>USA!</h5>
<br>
<h4>USA!</h4>
<br>
<h3>USA!</h3>
<br>
<h2>USA!</h2>
<br>
<h1>USA!</h1>
<br>
<% end %>
<h2 align="center"> HULKAMANIA IS RUNNIN' WILD BROTHA!</h2>
</body>
</html>
How to Pass Data from An Action to a View
Ideally you want to place your Ruby code inside of your controller, as the View is really more about displaying results than creating them. In this example we are going to tell the user which day of the year it is and then display the current date and time. To do so, open your controller file named weight and edit it so it reads as follows:
class WeightController < ApplicationController
def enormous
@a=Time.now
@day=@a.yday
end
end
Now go ahead and save it. When you are finished, open your enormous.rhtml file. Now we are going to display the results of our action:
<html>
<body>
This is the <%= @day %> day of the year.
<br>
The current date and time is: <%= @a %>
</body>
</html>
Don't worry too much if the code in your controller doesn't make a whole lot of sense. The important thing to know is the structure and layout of where the code goes. We will discuss Ruby functions, such as the Time.Now function shown above, in a future tutorial.
Your result will be different from mine; in fact, if you get exactly the same result I did, then there is something seriously wrong in the world. In any case, at the time of this writing, the result of the above code is:
This is the 347 day of the year.
The current date and time is: Thu Dec 13 15:18:57 -0500 2007
We create an instance variable named @a in our controller and assign it the value of the Time.now function (which is basically the current time according to your system time). We then create another variable named @day and assign it the value of the day of the year by extracting the data from Time.Now via the yday function. We then call upon the variables in our view to display the results.
Next: Creating Two Views and Choosing Between Them >>
More Ruby-on-Rails Articles
More By James Payne