Ruby-on-Rails
  Home arrow Ruby-on-Rails arrow Page 2 - Ruby On Rails: Making Your First Dynamic S...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Dedicated Servers  
Actuate Whitepapers 
Moblin 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
RUBY-ON-RAILS

Ruby On Rails: Making Your First Dynamic Site
By: James Payne
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 5
    2008-05-05

    Table of Contents:
  • Ruby On Rails: Making Your First Dynamic Site
  • Viewing Your Page
  • Creating Two Views and Choosing Between Them
  • Choosing Which View to View

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT

    AT&T devCentral & BlackBerry(r) Webcast Series: BlackBerry and GPS -Build Location Awareness into your BlackBerry Applications, July 10th -1:00PM EST. Register Today!

    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.

    More Ruby-on-Rails Articles
    More By James Payne


       · Thanks for stopping by to read another episode in my series on Ruby on Rails. Here...
     

    RUBY-ON-RAILS ARTICLES

    - Ruby On Rails: Making Your First Dynamic Site
    - Ruby on Rails: Beginning Rails
    - Ruby: Modules, Mixins, Fixins, and Rails
    - Controlling Information Access with the Rail...
    - URLs, Filters and the Rails Action Controller
    - Flash and the Rails Action Controller
    - Rails Action Controller
    - Dropping and Sorting with AJAX and script.ac...
    - Drag and Drop with script.aculo.us and Rails
    - Introducing script.aculo.us
    - Ruby Classes and Objects
    - Ruby Loops
    - Ruby Conditionals
    - Ruby Operators and Arrays
    - Ruby for the Newbie







    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway