Friday, October 30, 2009

Workshop 5

Part A:

1. Create the Rails application framework in the projects folder: C:\InstantRails\...\projects\>rails animals

Done.

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/

So, I originally set up the projects folder as C:\InstantRails2.0\rails_apps\projects, opened the Ruby console window, changed to that directory and ran the 'rails animals' command, all good up to here.

Trying to run the 'ruby script\server' command doesn't work (at least, not for me) when using this folder structure, neither does using the 'Manage Rails Applications' menu (it doesn't have any facility to open the projects folder. So, I figure that this folder structure doesn't work well for InstantRails.

I moved the projects folder to be C:\InstantRails2.0\projects and then changed to the animals subfolder. Issuing the 'ruby script\server' command worked, and the Mongrel server started. So now the web browser successfully displays the 'Welcome aboard' page.

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.



Done

4) Test the controller by starting the WEBrick server and navaigatibng 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\views\mammal directory.





7) Create and save a view in that directory by using a text editor to create a view called breathe.rhtml

Restart the WEBrick server and browse again at http://localhost:3000/mammals/breathe




Done

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:


5 + 6 =<%= 5 + 6 %>



=<% 4.times do %>
Inhale Exhale

<%end%>

Time is <%=Time.now %>



Done

Part B:


1) Create a new application called scenery in the same projects directory to demonstrate the use of an active view.

Done.

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

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

Done

6) Create a file in the public directory - \cabs\public called input.html


Done

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


Done

8) Edit the view template cabtype.rhtml


Done

9) Start the Web server and go to the opening page of this application at http://localhost:3000/input.html

Done

10) Submit the forms data. What do you find?

After getting this to work with the params hash of building1 as an associative array, I changed it to a drop-down list. I felt that it was more intuitive for a user (at least, for this user). I also made some cosmetic changes in the formatting in both the input.html and cabtype.html.erb files.

No comments:

Post a Comment