Wednesday, February 4, 2009

Passing Data To Views From Controllers

Often we're going to need to get information from the Controller to the Views and typically this data will come from Models. Here we're just going to set something in the Controller and then show how to use it in a View. We'll start with our code for Using page.xhtml. In each of the methods, we set the @title value. This value, accessed using #@title can then be used in the View, in this case page.xhtml to display. We use it to set the title and as a header. Here's the ruby code

require 'rubygems'
require 'ramaze'

# This example is based on the previous "multiple pages" example.
# It has the same three methods (index, page1, and page2) but adds
# links on each page to the other two pages. For linking it uses
# the Ramaze Rs helper for linking within the same controller. The
# other item that is different is that instead of the content
# being in double quotes, we use the %{} form which is the same
# as the double quotes or as %Q{}. They just make it easier to
# include quotes in the string.
#
# Additionally, this adds the use of a layout using
# page.xhtml from the view directory. The layout uses the @title
# set in each of the methods (index, page1, and page2) to
# put a title on the page and to set it in the <h1> tags.
class MainController < Ramaze::Controller

# Use page.xhtml in the view directory for layout
layout :page

# You can access it now with http://localhost:7000/
# This should output
# Hello, World!
# twice in your browser and provide links to the other two pages.
# The first time it uses the @title in the <h1> tags and the second
# comes from the content provided by the method.
def index
@title = "Index Page"
%{
Hello, World! <br/>
<a href="#{Rs(:page1)}">Page 1</a>
<a href="#{Rs(:page2)}">Page 2</a>
}
end

# You can access it now with http://localhost:7000/page1
# This should output
# "Page 1!"
# twice in your browser and provide links to the other two pages.
# The first time it uses the @title in the <h1> tags and the second
# comes from the content provided by the method.
def page1
@title = "Page 1"
%{
Page 1 <br/>
<a href="#{Rs(:index)}">Hello world</a>
<a href="#{Rs(:page2)}">Page 2</a>
}
end

# You can access it now with http://localhost:7000/page2
# This should output
# "Page 2!"
# twice in your browser and provide links to the other two pages.
# The first time it uses the @title in the <h1> tags and the second
# comes from the content provided by the method.
def page2
@title = "Page 2"
%{
Page 2 <br/>
<a href="#{Rs(:index)}">Hello world</a>
<a href="#{Rs(:page1)}">Page 1</a>
}
end
end

Ramaze.start


and here's page.xhtml

<html>
<head> <title>#@title</title> </head>
<body>
<h1>#@title</h1>
#@content
<h5> Powered by Ramaze </h5>
<
/body>
</html>


As before, put the page.xhtml into a view directory under where ever you put the ruby code passdatatoviews.rb. Then run using ruby passdatatoviews.rb

As always, feel free to leave questions in the comments section.

2 comments:

  1. hi slabounty ... that's good, but accessing single data is quite trivial ... suppose we have a page with a complex form with let's say 10 inputs, 2 selects, a few optionboxes, etc ... the question is: when we postback this form will we have to get each form element by requesting each single element one at a time or ramaze will encapsulate the form elements values in an object and we could then acess this object?

    ReplyDelete
  2. SohDubom, I'll be getting to an example like that in the near future. In the meantime you can check out using the "request" variable that is generated when you use a form. So the answer is, yes Ramaze encapsulates the data.

    ReplyDelete