Section 1.7. Tying the Controller to the View


1.7. Tying the Controller to the View

In MVC, the view usually renders model data provided by the controller. Let's set an instance variable in the controller and render it in the view. First, add an instance variable called @welcome_message to the controller:

 class GreetingController < ApplicationController    def index  @welcome_message = "Welcome to your first Rails application"  end end 

Now, display the new message in the view by adding a Ruby expression between <%= and %> tags. Rails renders the value of the expression within these tags, just as if the value of the expression had been printed in place. Here's a view that prints your welcome message as a level one heading:

 <h1><%= @welcome_message %></h1> 

Reload. You'll see the same output you got in Example 1-1, though the structure of the application is different. In Example 1-1, you rendered your view within the controller. Here, you built an RHTML template . Your HTML tags provided static structure and style, and your Ruby code provided dynamic content; in this case, a variable set within the controller.

1.7.1. Expressions and Scriptlets

When you're embedding Ruby code, you've got two options. Scriptlets are Ruby code, placed between <% and %> tags. Scriptlets rely on side effects, or the output of the Ruby code. Expressions are Ruby expressions placed between <%= and %> tags. The expression presents the value returned by the Ruby code.

You can experiment with the interaction between the controller and view. We've changed the controller and view for greeting to show a few examples of expressions and scriptlets in action. First, we'll set a few values in the controller:

 class GreetingController < ApplicationController   def index     @age=8     @table={"headings" => ["addend", "addend", "sum"],             "body"     => [[1, 1, 2], [1, 2, 3], [ 1, 3, 4]]            }   end end 

Next, here's the view showing expressions and scriptlets, with both interacting with values set in the controller:

 <h1>Simple expression</h1> <p>Tommy is <%= @age %> years old.</p> 

Now, display the value of the instance variable @age , which was set in the controller:

 <h1>Iteration using scriptlets</h1> <% for i in 1..5 %>   <p>Heading number <%= i %> </p> <% end %> 

Iterate with a scriptlet and show the current count with an expression:

 <h1>A simple table</h1> <table>   <tr>     <% @table["headings"].each do head %>       <td>         <b><%= head %></b>       </td>     <% end %>   </tr>   <% @table["body"].each do row %>     <tr>       <% row.each do col %>         <td>            <%= col %>         </td>       <% end %>     </tr>   <% end %> </table> 

Finally, use both techniques to display the contents of @table .

You'll get the results shown in Figure 1-5.

Figure 1-5. Results of embedded scriptlets and expressions



Ruby on Rails[c] Up and Running
Ruby on Rails[c] Up and Running
ISBN: B003D3OGCY
EAN: N/A
Year: 2006
Pages: 94

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net