《Agile Web Development with Rails》读书笔记(二)
来源:互联网 发布:网络预警系统 编辑:程序博客网 时间:2024/06/05 04:27
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.
- 《Agile Web Development with Rails》读书笔记(二)
- 《Agile Web Development with Rails》读书笔记(一)
- 《Agile Web Development with Rails》读书笔记(三)
- 《Agile Web Development with Rails》读书笔记(四)
- Agile Web Development with Rails 翻译(二)
- Agile Web Development with Rails
- Agile Web Development with Rails
- Agile Web Development with Rails 翻译(一)
- Agile Web Development with Rails 翻译(三)
- Agile Web Development with Rails 翻译(四)
- Agile Web Development with Rails 翻译(五)
- Agile Web Development with Rails 翻译(六)
- Agile Web Development with Rails 翻译(七)
- Agile Web Development with Rails 翻译(八)
- Agile Web Development with Rails 翻译(九)
- Agile Web Development with Rails 翻译(十)
- Agile Web Development with Rails 翻译(十一)
- Agile Web Development with Rails 翻译(十二 )
- Linux 内核剖析
- 高效率的3D图形数学库
- Fail to get datasource
- VC++中,配置文件的读写操作
- 百度篇——霸王面时递交的百度招聘会思考
- 《Agile Web Development with Rails》读书笔记(二)
- 让“显示所有的文件和文件夹”功能失效
- web开发,如何自动选定打印机进行打印(设置默认打印机打印)
- [技巧]如何绕过MaxtoCode的15天限制
- 金庸小说中的农业漏洞
- 灯火阑珊处
- SOA
- 软件测试工程师的招聘体会(一)
- windows 核心编程 线程