Ruby On Rails: Making Your First Dynamic Site (Page 1 of 4 )
In our last article we learned how to create a static web page with Rails and learned all about the workings of the grand Model-View-Controller architecture. In this article I am going to teach you how to create dynamic pages using Rails as your framework.
Making Our View Look Like a Mathematical Genius
We learned in the last article to create .rhtml files. These files allow you to embed Ruby code inside of them, and run the code prior to sending the file to the browser, using Embedded Ruby, or Erb. When we write Ruby code into our pages, we use the <% and %> tags, as shown in the example below:
<html>
<body>
<h1>Some math to calculate your enormity<h1>
<p>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</p>
<% 4000 * 9000000 %>
</body>
</html>
The above sample displays some text:
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
You will note that the math portion was not displayed. In order to display the result of our math, we would have to modify our tag a little, as shown here:
<html>
<body>
<h1>Some math to calculate your enormity<h1>
<p>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</p>
<%= 4000 * 9000000 %>
</body>
</html>
You can also mix up your HTML and Ruby, like so:
<html>
<body>
<h1>Some math to calculate your enormity<h1>
<p>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</p>
Your weight in pounds: <%= 4000 * 9000000 %>
</body>
</html>
So let's create a page from start to finish to display your enormous weight.
We start off by going to our Ruby directory and creating a new application called fatty, then switch to the fatty directory and create our controller named Weight, action, and view. Type the following into your prompt:
>rails fatty
>cd fatty
>ruby script/generate controller Weight
Now that we have created our framework and our controller, we are going to edit the weight.rb file and add an action named enormous to it. Open up the weight.rb file and modify the code so it reads in the following manner:
class WeightController < ApplicationController
def enormous
end
end
Finally, we will create our view to display our wondrous mathematical abilities. Open a notepad file and type in the following:
<html>
<body>
<h1>Some math to calculate your enormity<h1>
<p>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</p>
Your weight in pounds: <%= 4000 * 9000000 %>
</body>
</html>
Save the file as enormous.rhtml in the directory C:rubyfattyappviews.
Next: Viewing Your Page >>
More Ruby-on-Rails Articles
More By James Payne