AWebFactory
Demo App Part3

Let’s develop the AWDWR demo app further within the rails environment, and polish it off!

Dynamic loops in the view

Let’s add the following code to the view:

<% 3.times do %>
Ho!<br />
<% end %>
Merry Christmas!

<% 3.downto(1) do |count| %>
<%= count %>...<br />
<% end %>
Lift off!

Before we saw that anything between <%= and %> is interpreted directly as Ruby code and the result passed on to the view to display.

Now, the symbols <% and %> (without the ”=”) cause whatever is between them to be interpreted without the result immediately being passed on to the view. This is perfect for loop and conditional statements.

Again, find the appropriate /demo/app/views/say/hello.rhtml file (which automatically corresponds to the method hello of the SayController), add in the code and save your work:

In your browser you should be seeing:

html encoding helper function h()

Email: < %= h("Ann&Bill<frazers@isp.email>") %>

Added to the view code, that yields:

But what does the helper function h() actually do? To find out view the source code in your browser:

<p>
Email: John&amp;Mary&lt;Smith@greatfree.email&gt;
</p>

What the function does is to escape the html characters passed to it as the string parameter, rendering them as html entities.

The time now is…

We could add the following code to the view:

<p>
  It is now <%= Time.now %>
</p>

But here we are getting carried away, and violating the MVC principle of separating view and controller (basically because then changes to the brains behind multiple views can be accomplished through changes in only one place, the controller, and also because then code won’t be pathalogically mixed and stuck in anywhere, inviting more bugs).

So we put the code where it belongs, in the controller :


class SayController < ApplicationController
  def hello
    @time = Time.now
  end
end

and use the view to, well, display the values, using the Ruby interpretation for the purposes proper to the view, in this case receiving a dynamic value:


<p>
  It is now <%= @time %>.
</p>

In RadRails this can be done conveniently:

and we get the right time every time we refresh our browser:

Hey, time to commit to the repository!

Well, I think I can spare the time: right-click on the demo folder, do /Team/Commit, fill in the Commit info:

Hit enter, and you have secure commit to the repository, and that’s that.

The Object Soup revisited

You say hello, I say goodbye (Linking Pages)

With the same controller, we will add another method (goodbye) and a view to match, and then see how to properly link the pages.

Add this code to the SayController:


class SayController < ApplicationController
  def hello
    @time = Time.now
  end
  def goodbye
  end
end

Create a new view to match the new method (goodbye.rhtml) by right-clicking on the /demo/app/views/say folder and choosing /New/File from the menu. Suitable code:


<html>
  <head>
    <title>See You Later!</title>
  </head>
  <body>
    <h1>Goodbye!</h1>
    <p>
      It was nice having you here.
    </p>
  </body>
</html>

After saving both files, we should have something like this:

With the following results in the browser at http://localhost:3000/say/goodbye

In order to link the hello view to the goodbye view, we could simply use good old fashioned html hyperlink anchor tags; but then we would have to specify absolute or relative paths which could be subject to modification under varying deployment scenarios (subdomain, subdirectory, new Rails deployment schemes). So instead we choose the helper method link_to(), which links a specified hyperlink text to a specified action, or method:


Code snippet for hello.rhtml:
<p>
  Time to say
  <%= link_to "GoodBye!", :action => "goodbye" %>
</p>

The first parameter of the link_to method is the string to be displayed as the hyperlink, and the second parameter is a keyword parameter (symbol) for the method goodbye, expressed in the form of a colon followed by the word action, followed by the pointer arrow (”=>“) and the string representing the action (method) to be invoked.

We add that to the bottom of hello.rhtml, save our work and get the following result in our browser:

If we click on the link, we are taken to /say/goodbye

We make the corresponding change in goodbye.rhtml, specifying the hyperlink string “Hello” and the action “hello”. Then we can click back and forth between the two pages.

If everything works and we are satisfied, then we need to add the file goodbye.rhtml to version control (right click on it and choose that option: /Team/Add to Version Control) and then commit all our work as a new revision (right click on the folder “demo”, then do /Team/Commit; fill out a suitable comment identifying the revision, and then we are left with the following:

Notice that goodbye.rhtml has a “5” next to it, showing it forms part of revision 5 in the Subversion repository; and notice that goodbye.rhtml doesn’t have syntax coloring. That minor but annoying detail is caused by the fact that when it was created as a new file, RadRails didn’t know what kind of file it was going to be; if you close it and simply open it again, it will be opened by the rhtml editor.

This completes our demo app.

Content Copyleft Victor Kane (ProjectMaster) and awebfactory.com.ar
Email me at info at awebfactory dot com dot ar
Full content (RSS 2.0) and Headlines (RSS 2.0)