Home arrow Ruby-on-Rails arrow Ruby On Rails: Making Your First Dynamic Site
RUBY-ON-RAILS

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: 5 stars5 stars5 stars5 stars5 stars / 12
May 05, 2008
TABLE OF CONTENTS:
  1. · Ruby On Rails: Making Your First Dynamic Site
  2. · Viewing Your Page
  3. · Creating Two Views and Choosing Between Them
  4. · Choosing Which View to View

print this article
SEARCH DEVARTICLES

Ruby On Rails: Making Your First Dynamic Site
(Page 1 of 4 )

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.


blog comments powered by Disqus
RUBY-ON-RAILS ARTICLES

- Ruby-on-Rails Faces Second Security Flaw in ...
- Ruby 2.0 Prepped for February 2013 Release
- Why LinkedIn Switched from Ruby on Rails
- Adding Style with Action Pack
- Handling HTML in Templates with Action Pack
- Filters, Controllers and Helpers in Action P...
- Action Pack and Controller Filters
- Action Pack Categories and Events
- Logging Out, Events and Templates with Actio...
- Action Pack Sessions and Architecture
- More on Action Pack Partial Templates
- Action Pack Partial Templates
- Displaying Error Messages with the Action Pa...
- Action Pack Request Parameters
- Creating an Action Pack Registration Form

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials