Ruby on Rails: Beginning Rails - A View...To a Kill (Duhn Duhn Dun!!!)
(Page 3 of 4 )
That's right folks, articles with sound effects. I'll be here all week.
So far we have created a controller and an action, which handle requests from the user and respond to the requests, respectively. Now, with our view, we will display said results to the user.
In order to do so, we create what is called a skeleton or a template web page whose ultimate purpose is to display something to the user. Actions use templates to store and display information, as you will soon see.
Templates are named with the .rhtml extension. If you name them the same as the action, they are instantly connected. So, in our case, we will name our template file whatyoutalkingbout.rhtml.
Creating our View
Open up a text file and enter in this brilliant code:
<html>
<body>
<h2>Welcome to the What You Talking About Website</h2>
<br>
<p>Here we will discuss what things Willis could have possibly been talking about, and furthermore, why Arnold was so damned anxious to find out...</p>
</body>
</html>
Go ahead and save the file in the rubymyfirsttimeappviewswillis folder.
Now start your WEBrick server again, as shown above. And finally, open up a browser and go to http://localhost:3000/willis/whatyoutalkingbout
You should see the text:
Welcome to the What You Talking About Website
Here we will discuss what things Willis could have possibly been talking about, and furthermore, why Arnold was so damned anxious to find out...
Congratulations on your first Rails web application.
How it all Works
When the user enters a URL, a request is sent to your web server, which is then decoded and sent to your controller. The controller then calls upon the actions that you create and passes the request on to the appropriate action. The action is determined by the URL. In our example, the action displayed a template with some data. Whoopee!
Next: What Everything is For >>
More Ruby-on-Rails Articles
More By James Payne