您的位置:首页 > 产品设计 > 产品经理

《Agile Web Development with Rails》读书笔记(二)

2007-09-19 15:06 537 查看
Instant Gratification
After the Rails environment has been established, I write a simple application to verify we have got Rails snugly installed on my machine.
I. Create a new project
Rails application is a bunch of Ruby source code, but does a lot of magic behind the curtain to minimum the explicit configuration.
I’ll use a new command-line tool rails to create my first project.
To create the first Rails application, pop open a shell window, navigate to a place in the file system where I want to create the application directory structure.
work>rails demo
This command creates a bunch of files and subdirectories, you can cd demo to have a look at it.
Two directories will be used in this example:
app: the kernel of MVC, put application code files here.
script: contain some useful utility scripts.
In order to see the result of create application, I start the stand-alone develop web server:
work>ruby script/server
note: this command will hang up the command shell, so open another shell window, change to the application directory to run the above command.

The service start on port 3000. I access the application using the URL
http://127.0.0.1:3000
the work of “create new application” is finished when I see the “welcome aboard” page in the browser.
Now, let’s write some content for this application.
II. Hello Rails!
Before I write some content for the application, first I introduce some concept.
The Architecture of Rails:
Rails accept a request from browser, decodes the request to find the controller, and call the action method in that controller. The controller then invoke a particular view to display the result to the user.
Rails and Request URLs
Like other web application, Rails application appears to its users to be associate with a URL.
I’ll take a URL as an example to explain it:



Rails uses the path to determine the name of the controller to use and the name of the action to invoke in that controller.
My First Action
Ok, now let’s write some content for our application.
Followed the discussion in the previous section, the hello action means create a method in the class SayController.
The controller’s job is to set up things so that the view knows what to display.
work/demo/app/controllers/say_controller.rb
class SayController > ApplicationController
def hello
end
end
work/demo/app/views/say/hello.rhtml
<html>
<head>
<title>Hello, Rails!</title>
</head>
<body>
<h1>Hello from Rails!</h1>
</body>
</html>
Save these two files, and refresh the browser, then you should see my friendly greeting.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: