Sunday, May 23, 2010

Workshop 5 - Part B

Part B: The active view: passing data from an action too a view
1. Create a new application called scenery in the same projects directory to demonstrate the use of an active view.














2. Create a controller called Demo in scenery\app\controllers
















3. Add an action to demo_controller.rb as the method called rubycobe










4. Add a view template - scenery\app\views\demo\rubycode.rhtml We will edit this view in later steps but you may like to add your own test HTML code to the view at this stage.



























5. Save and restart the Web server and navigate to http://localhost:3000/scenery/rubycode


6. Use the Time.now example to pass data from an action to a view.


7. Modify and save the rubycode action with a value for the time instance variable in the DemoController class in app\controllers\demo_controller.rb

class DemoController< ApplicationController

def rubycode

@time_now = Time.now

end

end

8. Then modify and save the corresponding view template in \app\views\demo\rubycode.rhtml by adding a call by reference to the action’s instance variable:

There was an issue in the sample code as the variable name in the rubycode controller was called time_now, but in the example of the rhtml file it was @time.now which would throw an error so i changed the rhtml to the following.

The time is <%= @time_now %>


9. Restart the Web server and navigate the browser to http://localhost:3000/demo/rubycode


No comments:

Post a Comment