Home Ruby-on-Rails Ruby On Rails: Making Your First Dynamic Site

# Ruby On Rails: Making Your First Dynamic Site

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.

Author Info:
By: James Payne
Rating:  / 13
May 05, 2008

SEARCH DEVARTICLES

 Ruby On Rails: Making Your First Dynamic Site (Page 1 of 4 )Making Our View Look Like a Mathematical GeniusWe 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:

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

<% 4000 * 9000000 %>The above sample displays some text: Some math to calculate your enormityWe will use two numbers: The first represents your physical weight, while the second represents your implied weight, which is determined by your effect on gravityYou 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:

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

<%= 4000 * 9000000 %>You can also mix up your HTML and Ruby, like so:

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: <%= 4000 * 9000000 %>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 WeightNow 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 < ApplicationControllerdef enormousendendFinally, we will create our view to display our wondrous mathematical abilities. Open a notepad file and type in the following:

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