Topic objectives:
- To reflect on what has been done so far with Ruby on Rails;
- To reflect on my role and decide to continue as EITHER a Developer OR as an IT infrastructure manager;
- To complete either the Developer’s Thread exercises or the IT Infrastructure manager’s Thread exercises below, according to my new role (NOT BOTH).
- To work with others in the same role via the subject forum or similar learning tool.
- To read and find out what those who chose the other role are doing each of the workshops 5 to 8 and discussing on the subject forum or learning tool.
- To be willing to change roles and share perspectives as developers learn from managers and vice versa.
DEVELOPERS THREAD (RED team)
- To create a Rails application framework with the WEBbrick or Mongrel Web server and:
- generate a controller and an action by adding a method(s) to a controller;
- create a view template for each action and to link to actions from views;
- use Rails for building applications with document requests, screen layouts and forms processing.
To share your findings with others.
To do:
Part A: Viewing the action
1. Create the Rails application framework in the projects folder: C:\InstantRails\…\projects\>rails animals
Created projects folder and ran >rails animals to create the rails application there.
2. Running the application on localhost:3000 using the WeBrick ruby server (or Mongrel as alternative) and access via Web browser at http://localhost:3000/
3. Create the controller to make the application do an action. This is under the controller-action/model-view structure.
Stop the WEBrick server each time you edit Ruby classes and then re-start or refresh the views you are testing. Use the Ruby command below:
>ruby script/generate controller Mammal
The mammal_controller.rb contains just a bare class description:
class MammalController< ApplicationController
end
and the ApplicationController class inherits from ActionController::Base class in the ActionController module under Rails.
4. Test the controller by starting the WEBrick server and navaigating the browser to http://localhost:3000/mammal Note how the controller name is appended to the end of the URL and that no action resulted because there are no controller methods.
5. Create an action by editing and saving the mammal_controller.rb class in projects\animals\app\controllers using your text editor to add the method below:
class MammalController< ApplicationController
def breathe
end
end
Done.
6. Start the WEBrick server and browse at http://localhost:3000/mammals/breathe where you will get a “missing template” message since it is missing a view for the breathe method.
Rails is trying to connect the breathe method action of the mammal controller to a view, by using the action’s name – breathe. This view template is created as breathe.rhtml and stored in the \projects\animals\app\views\mammal directory.
7. Create and save a view in that directory by using a text editor to create a view called breathe.rhtml
<html>
<head>
<title>Breathe Easy</title>
</head>
<body>Inhale and Exhale
</body>
</html>
Restart the WEBrick server and browse again at http://localhost:3000/mammals/breathe
Didn’t work as the above is incorrect – must change the url from mammals to mammal and rename the html file for Rails 2.0 to breath.html.erb
8. Try Ruby code and HTML in the action view by using the <%….%> wrapper around the inserted Ruby code. Here are some snippets to try from workshop 4:
<br>
5 + 6 =<%= 5 + 6 %>
</br>
|
<br>
=<% 4.times do %>
Inhale Exhale <br>
<%end%>
|
<br>
Time is <%=Time.now %>
</br>
|
NOTE: in practise you normally perform calculations in the action (method) and pass the results to the view
Part B : The active view: passing data from an action to a view
1. Create a new application called scenery in the same projects directory to demonstrate the use of an active view.
> rails scenery
> cd scenery
Done.
2. Create a controller called Demo in scenery\app\controllers
scenery> ruby script/generate controller Demo
Done.
3. Add an action to demo_controller.rb as the method called rubycobe
class DemoController< ApplicationController
def rubycode
end
end
Done.
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.
Done rubycode.html.erb
5. Save and restart the Web server and navigate to http://localhost:3000/scenery/rubycode
Hmm getting a routing error…should by /demo/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
Done.
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:
<br>
The time is <%= @time.now %>
<br>
@time.now should be @time_now
9. Restart the Web server and navigate the browser to http://localhost:3000/demo/rubycode
Data has been passed from the action to the view as it is done with SQL requests. The instance variables of a Ruby class are available to view templates by referencing the action’s instance variables by name in the view .rhtml template.
Part C: Screen layouts and forms processing with text fields, check boxes, radio buttons and multiple list controls
1. Create a new application called cabs in the same projects directory to demonstrate the use of an active view.
> rails cabs
> cd cabs
Done.
2. Create a controller called Vehicle in cabs\app\controllers
cabs> ruby script/generate controller Vehicle
Done.
3. Add an action to vehicle_controller.rb as the method called cabtype
class VehicleController< ApplicationController
def cabtype
end
end
Done.
4. Add a view template – cabs\app\views\vehicle\cabtype.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.
Done.
5. Save the view and restart the Web server and navigate to http://localhost:3000/cabs/cabtype
Should be /vehicle/cabtype!
6. Create a file in the public directory – \cabs\public called input.html
Please note that the following includes fixes identified on other blogs!
7. Edit the vehicle_controller.rb here is a start. The data in each form element in the Rails application can be accessed via its name and a hash called params
class VehicleController< ApplicationController
def cabtype
@data1 = params[:text1]
@data2 = params[:check1]
@data3 = params[:radios1]
@data4 = params[:building1]
end
end
8. Edit the view template cabtype.rhtml
9. Start the Web server and go to the opening page of this application at http://localhost:3000/input.html
10. Submit the forms data. What do you find?
I can’t get it to work despite all the felp from other blogs!
11. Report your progress or findings in your Developers Blog
This whole section needs fixing to remove all the frustrating errors – will that taxi ever arrive?