After madly writing endless lines of Java code for the past three weeks that does next to nothing (see my previous rant) I'm elated to see that there are others in the world who believe less code is better.
To demonstrate, here are the steps you can take to create a dynamically generated page with Ruby on Rails.
1. > rails ./demo
2. > cd demo
3. > mv public/index.html public/index.html.old
4. > script/generate controller people list
5. modify config/routes.rb and add the line map.connect '', :controller => 'people', :action => 'list'
6. modify app/controllers/people_controller.rb (see listing below)
7. modify app/views/people/list.rhtml (see listing below)
8. > script/server
9. browse to localhost:3000
10. voila!
A couple of important points to notice is that Rails embraces the MVC design pattern. Model and Controller classes are separate while the View is off in its own place. The view is an .rhtml file because it's basically HTML with Ruby embedded within it. While those who've been doing JSP development may scoff at the mere idea of embedding "code" within a page and believe that custom tags is the only way to go, I have to admit that after doing JSF for a few weeks I actually like this a whole lot better. Remembering that Ruby is a 'scripting' language and that Rails uses Controller classes (like "code-behind") may help people get over their prejudice. Finally here a few links I've found very valuable during the last week:
What Is Ruby on Rails
Rolling With Ruby on Rails
Rolling with Ruby on Rails, Part 2
Ruby On Rails Documentation
why's poignant guide to Ruby
Rails Academy (videos)
app/controllers/people_controller.rb
class PeopleController < ApplicationController
def list
@names=['Yukihiro Matsumoto', 'David Heinemeier Hansson']
end
end
app/views/people/list.rhtml
<html>
<head>
<title>This is my first Ruby page</title>
<link rel="stylesheet" type="text/css"
href="stylesheets/mystyle.css" />
</head>
<body>
<h1>Ruby/Rails People List</h1>
<table>
<% @names.each do |name| %>
<tr><td><%= name %></td></tr>
<% end %>
</table>
</body>
</html>
No comments:
Post a Comment