Tuesday, June 23, 2009

Workshop 5 - Part A: Viewing the action

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

Created the Rails application called animals.

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/

Started the animals application. I am using Mongrel as the web server. The welcome aboard page is displayed.

3. Create the controller to make the application do an action. This is under the controller-action/model-view structure.

Stop the Mongrel server each time you edit Ruby classes and then re-start or refresh the views you are testing. Use the Ruby command below:

Changed directory into the aminals folder - cd animals.

>ruby script/generate controller Mammal

Generated the controller called Mammal.

The mammal_controller.rb contains just a bare class description:

class MammalController < ApplicationController
end

Checked the contents of the mammal_controller.rb file.

4. Test the controller by starting the Mongrel server and navigating 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.

Completed. Got the message: No action responded to index.


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

Added the breathe method to the mammal_controller.rb class.


6. Start the Mongrel 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.

Completed. Got the message "Missing template mammal/breathe.html.erb in view path C:/INSTAN~1/rails_apps/animals/app/views"

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 Mongrel server and browse again at http://localhost:3000/mammals/breathe

This didn't work. The file must be called breathe.html.erb. This is the Rails 2.0 format.
Renamed the file and the page rendered correctly.

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 %>

Completed. Here is the output:


NOTE: in practise you normally perform calculations in the action (method) and pass the results to the view.

No comments:

Post a Comment