Ruby-on-Rails
  Home arrow Ruby-on-Rails arrow 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  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
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 / 8
    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


    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.

    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

    - Iterating and Incrementing Strings in Ruby
    - Comparing and Manipulating Strings in Ruby
    - Strings in Ruby
    - 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







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek