Sunday, July 09, 2006

Hello, PuneRuby from Rails

Rails being an MVC framework, accepts incoming requests from a browser, decodes the request to find a controller, and calls an action method in that controller. The controller then invokes a particular view to display the results back to the user. Rails takes care of most of the internal plumbing that links all these things together. To write our simple Hello, PuneRuby application, we need code for a controller and a view. We don't need code for a model, as we're not dealing with any data.

Let's use a generator script to create a new controller for our application. This command is called generate, and it lives in the script subdirectory of the demo folder we created. Let's create a controller called Say as follows:
c:/rails/work/demo>ruby script/generate controller Say
The source file we'll be interested in is the controller. You'll find the file say_controller.rb in the folder:
app/controllers/say_controller.rb. Let's have a look at it.
class SayController < ApplicationController
end
SayController is an empty class that inherits from ApplicationController, so it automatically gets all the default controller behavior. We need to add some code to have our controller handle the incoming request. What does this code have to do? For now, it'll do nothing - we simply need an empty action method. Let's add an action called hello to our say controller. Adding a hello action means creating a method called hello in the class SayController. A controller's job is to set up things so that the view knows what to display. In our first application, there's nothing to set up, so an empty action will work fine. Modify say_controller.rb as follow:
class SayController < ApplicationController
def hello
end
end
Now let's try calling it. Find a browser window, and navigate to the URL:
http://localhost:3000/say/hello.
You will see in your browser, a message saying - "Template is Missing." This happens because we created the controller class and the action method, but we haven't told Rails what to display.

By default, Rails looks for templates in a file with the same name as the action it's handling. In our case, that means we need to create a file called hello.rhtml in the folder:
app/views/say/hello.rhtml as follow:
<html>
<head>
<title>Hello, PuneRuby</title>
</head>
<body>
<h1>Hello from PuneRuby!</h1>
</body>
</html>
Save the file hello.rhtml, and refresh your browser window. You should see it display our friendly greeting. Notice that we didn't have to restart the application to see the update. During development, Rails automatically integrates changes into the running application as you save files.

The .rhtml suffix tells Rails to expand the content in the file using a system called ERb (for Embedded Ruby). ERb is a filter that takes an .rhtml file and outputs a transformed version. The output file is often HTML in Rails, but it can be anything. Normal content is passed through without being changed. However, content between <%= and %> is interpreted as Ruby code and executed. The result of that execution is converted into a string, and that value is substituted into the
file in place of the <%=...%> sequence. Here's the modified hello1.rhtml file. In addition, stuff in rhtml between <% and %> (without an equals sign) is interpreted as Ruby code that is executed with no substitution back into the output. The interesting thing about this kind of processing, though, is that it can be intermixed with non-Ruby code. Refresh your browser to see the result.

Let's see how we can link two pages. Add a bye method to the file say_controller.rb as follows:
class SayController < ApplicationController
def hello
end
def bye
end
end
and create the relevant bye.rhtml file as follows:
<html>
<head>
<title>Bye for now</title>
</head>
<body>
<h1>Bye for now</h1>
</body>
</html>


Fire up your browser again, but this time point to the new view using the URL
http://localhost:3000/say/bye.

Now we need to link the two screens together. We'll put a link on the hello screen that takes us to the bye screen, and vice versa. For now we'll use hyperlinks. Rails comes with a bunch of helper methods that can be used in view templates. Here, we'll use the helper method link_to( ), which creates a hyperlink to an action. The file hello2.rhtml shows this:
<html>
<head>
<title>Hello, PuneRuby</title>
</head>
<body>
<h1>Hello from PuneRuby</h1>
<!-- code added for third part of tutorial -->
<br />
<br />
<%= Time.now %>
<br />
<p>
Time to say
<%= link_to "Bye", :action => "bye" %>
</p>
<!-- code added for third part of tutorial -->
</body>
</html>

There's a link_to( ) call within an ERb <%=...%> sequence. This creates a link to a URL that will invoke the bye( ) action. The first parameter in the call to link_to( ) is the text to be displayed in the hyperlink, and the next parameter tells Rails to generate the link to the bye action. As we don't specify a controller, the current one will be used.

Assignment: Create a .rhtml page that links to our page hello2.rhtml Post your solution as comment to this blog post.

First Post | Previous | Next



Technorati Tags:
Blogs linking to this article

8 Comments:

Blogger Unknown said...

Sandip, when posting to blog the less than sign should be < and the greater than sign shoul;d be >

11:51 AM  
Blogger Unknown said...

Deepali, can you post the exact error, line no etc

11:51 AM  
Blogger Unknown said...

Like I said before, replace all less than characters by (ampersand character)lt; and greater than characters by (ampersand character)gt; and then post here.

2:21 PM  
Blogger Unknown said...

Raj, use ampersandlt; and ampersandgt; where ampersand should be replaced by the symbol of ampersand

5:01 PM  
Blogger Unknown said...

Nitin, glad to know that atleast someone is reading my blog post! You are right, it should be:
http://localhost:3000/say/bye

Thanks, I have corrected this.

11:04 AM  
Blogger Aspirations said...

<html>
<head>
<title>Hello</title>
</head>
<body>
<h1>Hello from Anish</h1>
<!-- code added for third part of tutorial -->
<br /> <br />
<%= 'The time now is: ' + Time.now.to_s %>
<br />
<p> Time to go to
<%= link_to "Pune Ruby", :action => "hello2" %>
</p>
<!-- code added for third part of tutorial -->
</body>
</html>

2:27 PM  
Blogger Ashish Kulkarni said...

Dear all,

I have been struggling with firewall issues while installing Rails.

Eventually, I have found a way of doing so. And I have created an entry in my blog. Click here to view it.

Hope this helps people to get onto Rails. Why don't they have a single installable like JDK for Ruby? Or am I not supposed to ask that?

Satish,

I am reading the blog. However, the ruby websites are all shite. No proper documentation.

I hope we can remedy this in the PuneRuby website.

Regards,

Ashish.

5:57 PM  
Blogger Ashish Kulkarni said...

Satish,

Controller code

<b>say_controller.rb:</b>

class SayController < ApplicationController
def hello
end
def hello2
end
def bye
end
end

View code

<b>hello.rhtml:</b>

<html>
<head>
<title>Hello, PuneRuby</title>
</head>
<body>
<h1>Hello from PuneRuby!</h1>
<%= link_to "Continue", :action => "hello2" %>
</body>
</html>


<b>hello2.rhtml:</b>

<html>
<head>
<title>Hello, PuneRuby</title>
</head>
<body>
<h1>Hello from PuneRuby</h1>
<!-- code added for third part of tutorial -->
<br />
<br />
<%= Time.now %>
<br />
<p>
Time to say
<%= link_to "Bye", :action => "bye" %>
</p>
<!-- code added for third part of tutorial -->
</body>
</html>


<b>bye.rhtml:</b>

<html>
<head>
<title>Bye for now</title>
</head>
<body>
<h1>Bye for now</h1>
</body>
</html>

Regards,

Ashish.

6:29 PM  

Post a Comment

<< Home